PHP
  Home arrow PHP arrow Page 5 - PHP: The Switch Statement and Arrays
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Developerworks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

PHP: The Switch Statement and Arrays
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-01-07

    Table of Contents:
  • PHP: The Switch Statement and Arrays
  • The Variable's Big Brother
  • Printing the Array Values
  • Associative Arrays
  • The Multidimensional Array
  • Beyond Multidimensional Arrays

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    PHP: The Switch Statement and Arrays - The Multidimensional Array
    (Page 5 of 6 )

    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


       · There is a mistake in codeinstead of $mygirlfriends[1] . “and” . $names[2]...
       · Hey thanks for catching that. They tie me up in a dungeon and force me to write...
     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway