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
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: