The 0th value of the array \$tv is $tv[0]

"; print "

The 1th value of the array \$tv is $tv[1]

"; // Let's modify an element (overwrite a slot) $tv[1] = "The Daily Show"; print "

The 0th value of the array \$tv is $tv[0]

"; print "

The 1th value of the array \$tv is $tv[1]

"; // Some tricks $n = count($tv); // how many elements are in the array? print "

There are $n elements in the array labeled \$tv

"; $ten = range (1, 10); // create an array 1,2,3,4,5,6,7,8,9,10 $days = range(1,31); $years = range(2011,2021); // Notice that we can't see array contents simply by printing the array // The following commands will simply print to the browser: "$ten = Array" etc. print "

Here is what happens if we try to print the contents of the array as if it were a mere variable:

"; print "

\$ten = $ten

"; print "

\$days = $days

"; print "

\$years = $years

"; // One way to reveal the contents of an array is to use the print_r command // Notice that this is a stand-alone PHP command; there are no quotes. /* According to the PHP manual, "print_r() displays information about a variable in a way that's readable by humans. " */ print "Here is what the \$months array looks like if we print the contents using print_r
"; print_r($months); print "
"; /* Some additional useful ways of manipulating arrays sort(); // sort by value asort(); // sort by value while maintaining keys ksort(); // sort by key shuffle(); // randomize order of array! also rsort, arsort, krsort to reverse (look these up to learn more!) */ // Examples // randomize print "
randomize an array
"; $test = array("cake","cookies","lies","coffee"); shuffle($test); print_r($test); // sort print "
sort an array
"; sort($test); print_r($test); // Select a number at random $test = range(1,10); shuffle($test); $pick = $test[0]; print "

I picked $pick at random from values ranging from 1 to 10.

"; // A fun application // of some of these ideas print "

What month were you born?

"; print ""; ?>