/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
Building a Multidimensional Array Here is how our $arrBooks example from last week’s article can be expanded into a multidimensional array: <?php ‘Title’=>‘Superman’, ‘Author’=>’Jerry Siegel and Joe Shuster’, ‘Title’=>‘Dune’, ‘Author’=>’Frank Herbert’, ‘Title’=>‘The Hobbit’, ‘Author’=>’J.R.R. Tolkien’, ‘Title’=>‘Carrie’, ‘Author’=>’Stephen King’, ); ?> Extracting Elements from a Multidimensional Array To extract a single element from the multidimensional array, you must refer to the keys in both the outer and inner arrays. For instance, the PHP code below: <? echo $arrBooks[‘Science Fiction][‘Title’]; echo "<br>"; echo $arrBooks[‘Horror’][‘Author’]; ?> would display: Dune Stephen King Looping Through a Multidimensional Array The easiest way to loop through a multidimensional array is to nest two foreach loops; the outer loop goes through each outer array element, and the inner loop goes through each inner array element within the selected outer element. <? foreach ($arrBooks as $obj_key =>$book) { echo "$obj_key Book:<br>"; foreach ($book as $key=>$value){ echo "$key: $value<br>"; } echo "<br>"; } ?> The display will look like this: Comic Book: Title: Superman Author: Jerry Siegel and Joe Shuster Publication Date: 1938 Science Fiction Book: Title: Dune Author: Frank Herbert Publication Date: 1965 Fantasy Book: Title: The Hobbit Author: J.R.R. Tolkien Publication Date: 1937 Horror Book: Title: Carrie Author: Stephen King Publication Date: 1974 Array Functions Arrays are one of the most useful variable types. Along with its versatility, arrays also can use a variety of functions. In the previous lesson, we used the is_array function to determine if a variable was an array and the sort function to sort the elements of an array. Here are some more examples of array functions. count($array): Counts the number of elements in an array. <? echo "There are $numBooks books in the collection.<br>"; ?> There are 4 books in the collection. extract($array): Converts associative array keys into string variables. The values of each key become the values of each variable. <? ‘ScienceFiction’ => ‘Dune’, ‘Fantasy’ => ‘The Hobbit’, ‘Horror’ => ‘Carrie’); // $arrBooks[‘Comic’] becomes $Comic // $arrBooks[‘ScienceFiction’] becomes $ScienceFiction // $arrBooks[‘Fantasy] becomes $Fantasy // $arrBooks[‘Horror] becomes $Horror echo "$Comic is a comic book.<br>"; echo "$Fantasy is a fantasy book.<br>"; ?> Superman is a comic book. The Hobbit is a fantasy book. extract($array, EXTR_PREFIX_ALL, ‘prefix’): Adds a prefix to the string variable to differentiate between arrays that have the same keys. <? ‘Comic’ => ‘Superman’, ‘ScienceFiction’ => ‘Dune’, ‘Fantasy’ => ‘The Hobbit’, ‘Horror’ => ‘Carrie’); // $arrBooks[‘Comic’] becomes $books_Comic // $arrBooks[‘ScienceFiction’] becomes $books_ScienceFiction // $arrBooks[‘Fantasy] becomes $books_Fantasy // $arrBooks[‘Horror] becomes $books_Horror echo "$books_Comic is a comic book.<br>"; echo "$books_Fantasy is a fantasy book.<br>"; ‘Comic’ => ‘Superman Returns’, ‘ScienceFiction’ => ‘Terminator’, ‘Fantasy’ => ‘Dark Crystal’, ‘Horror’ => ‘Friday the 13th’); // $arrFilms [‘Comic’] becomes $films_Comic // $arrFilms [‘ScienceFiction’] becomes $films_ScienceFiction // $arrFilms [‘Fantasy] becomes $films_Fantasy // $arrFilms [‘Horror] becomes $films _Horror echo "$films_Comic is a comic film.<br>"; echo "$films_Fantasy is a fantasy film.<br>"; ?> Superman is a comic book. The Hobbit is a fantasy book. Superman Returns is a comic film. Dark Crystal is a fantasy film. compact(var1, var2, var3): Converts a list of variables into an array. <? $Comic = ‘Batman’; $ScienceFiction = ‘Dreaming Void’; $Fantasy = ‘American Gods’; $Horror = ‘Frankenstein’; foreach ($arrBooks2 as $key => $value) { print "$value is an example of a $key book.<br>\n"; } ?> Batman is an example of a Comic book. Dreaming Void is an example of a ScienceFiction book. American Gods is an example of a Fantasy book. Frankenstein is an example of a Horror book.