modify1($a1); modify1($a2); echo "<h3>After modify1 the arrays are:</h3>"; showa($a1); showa($a2); // modify2 will modify the parameters within the functions but leave the // argument arrays unchanged modify2($a1); modify2($a2); echo "<h3>After modify2 the arrays are:</h3>"; showa($a1); showa($a2); // modify3 will modify the argument arrays modify3($a1); modify3($a2); echo "<h3>After modify3 the arrays are:</h3>"; showa($a1); showa($a2); showaRec($a2, 0); echo "<hr/>\n"; // Demonstrating local vs. global variables globloc(); echo "Back in main: <br/>\n"; print_r($a1); echo "<br/>\n"; print_r($a2); echo "<br/>\n"; ?> </body> </html>
// Note that $a1 and $a2 below have identical keys and values. // However, the actual arrays are not identical -- see output $a1 = array(10, 20, 30, 40, 50); $a2 = array(2 => 30, 4 => 50, 0 => 10, 3 => 40, 1 => 20); // $a3, $a4 and $a5 have "non-traditional" keys, at least for Java or // C++ programmers. Be careful when using these types of keys $a3 = array(9 => 10, 10 => 20, 11 => 30, 12 => 40, 13 => 50); $a4 = array("zero" => 10, "one" => 20, "two" => 30, "three" => 40, "four" => 50); $a6 = array("2" => 30, "4" => 50, "0" => 10, "3" => 40, "1" => 20); // Iterate using the foreach loop. Note the orderings. showa($a1); showa($a2); showa($a3); showa($a4); showa($a5); showa($a6); // Iterate using "traditional" for loop. Note some missing values. showb($a1); showb($a2); showb($a3); showb($a4); showb($a5); showb($a6); // The print_r function will print out all of the contents of a PHP // array. print_r($a1); echo "<br/>\n"; print_r($a2); echo "<br/>\n"; print_r($a3); echo "<br/>\n";
{ for ($i = 0; $i < count($a); $i++) { for ($j = 0; $j < count($a[$i]); $j++) { echo $a[$i][$j], " "; } echo "<br/>\n"; } } $a1 = array(10, 20, 30, 40, 50); $a2 = array("here", "is", array("an", "array", array("with", "several"), array("levels", "of")), array("nesting", "it", "is"), "quite", "wacky"); $a3[0] = array(1, 2, 3, 4); $a3[1] = array(5, 6, 7, 8); $a3[2] = array(9, 10, 11, 12); showa($a1); showa($a2); showa($a3); echo "<hr/>\n"; showMat($a2); // Note the odd output for this call. Be sure you // understand why it appears as it does showMat($a3); // This output looks more natural, since the data is stored // in the traditional "matrix" fashion // Note that neither method (showa, showMat) produces acceptable output for // $a2. We will soon see how to process the array so that all of the data // is accessed. One predefined way to show an array with arbitrary nesting // is the print_r method as shown below. echo "<hr/>\n"; print_r($a1); echo "<br/><br/>\n"; print_r($a2);
// Let's look at some different arrays and how they are accessed // Note that $a1 and $a1 below have identical keys and values. // However, the actual arrays are not identical -- see output $a1 = array(10, 20, 30, 40, 50); $a2 = array(2 => 30, 4 => 50, 0 => 10, 3 => 40, 1 => 20); // $a3, $a4 and $a5 have "non-traditional" keys, at least for Java or // C++ programmers. Be careful when using these types of keys $a3 = array(9 => 10, 10 => 20, 11 => 30, 12 => 40, 13 => 50); $a4 = array("zero" => 10, "one" => 20, "two" => 30, "three" => 40, "four" => 50); $a5 = array("zero" => 10, 1 => 20, 2 => 30, "three" => 40, 4 => 50); // Iterate using the foreach loop. Note the orderings. showa($a1); showa($a2); showa($a3); showa($a4); showa($a5); // Iterate using "traditional" for loop. Note some missing values. showb($a1); showb($a2); showb($a3); showb($a4); showb($a5); // The print_r function will print out all of the contents of a PHP // array. print_r($a1); echo "<br/>\n"; print_r($a2); echo "<br/>\n"; print_r($a3); echo "<br/>\n"; print_r($a4);