The most enjoyable array is the multidimensional array. It lets you store an array inside of an array inside of an array and so forth. The best way to explain it is to show it in use, so here we go: <html> <body> <?php $heroesandvillains = array ( “Super Friends”=>array ( “Superman”, “Wonder Woman”, “Batman”, “The Wonder Twins”, “Aqua Man”, “Black Vulcan”, “Apache Chief”, “Robin”, “Green Lantern”, “Hawkman”, “That Monkey Whose Name I Can't Remember” ), “Legion of Doom”=>array ( “Lex Luthor”, “Solomon Grundy”, “The Riddler”, “Cheetah”, “Bizarro”, “Black Manta”, “Scarecrow”, “Sinestro” “Gorilla Grodd”, “Brainiac”, “Giganta”, “Toyman” ), “Ancillary”=>array ( “Mr. Mxyzptlk” ) ); } ?> The above code creates a multidimensional array named $heroesandvillans and then creates a series of sub-arrays within it (Super Friends, Legion of Doom, and Ancillary). Finally, values were assigned to each sub-array. Let's extract some data from the above sample: <html> <body> <?php $heroesandvillains = array ( “Super Friends”=>array ( “Superman”, “Wonder Woman”, “Batman”, “The Wonder Twins”, “Aqua Man”, “Black Vulcan”, “Apache Chief”, “Robin”, “Green Lantern”, “Hawkman”, “That Monkey Whose Name I Can't Remember” ), “Legion of Doom”=>array ( “Lex Luthor”, “Solomon Grundy”, “The Riddler”, “Cheetah”, “Bizarro”, “Black Manta”, “Scarecrow”, “Sinestro” “Gorilla Grodd”, “Brainiac”, “Giganta”, “Toyman” ), “Ancillary”=>array ( “Mr. Mxyzptlk” ) ); echo “Oh no it's “ .$superheroesandvillains['Ancillary'][0] . “!”; } ?> If printed, this code would print: Oh no it's Mr. Mxyzptlk!
blog comments powered by Disqus |
|
|
|
|
|
|
|