Home arrow PHP arrow Page 5 - PHP: The Switch Statement and Arrays

The Multidimensional Array - PHP

In our last exciting adventure (back in early November), we braved crocodiles, ravenous editors, most of the PHP statements, and beginning loops. In this edition we'll cover the final statement, the Switch, and discuss arrays. So sit back, order your R2D2 robot to bring you a cold, frosty Jolt Cola, and let's get cracking.

TABLE OF CONTENTS:
  1. PHP: The Switch Statement and Arrays
  2. The Variable's Big Brother
  3. Printing the Array Values
  4. Associative Arrays
  5. The Multidimensional Array
  6. Beyond Multidimensional Arrays
By: James Payne
Rating: starstarstarstarstar / 6
January 07, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By James Payne
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap

Dev Shed Tutorial Topics: