Oct
14
2008

ZCE Array: Multi-Dimensional Arrays

In this section of the PHP 5 Zend Certification Blog I explain Multi-Dimensional Arrays, put simply its an array inside another array. At first it is confusing but if you start using arrays you will find them useful and very effective.

Array does not have to be a simple list of keys and values; each array element can contain another array as a value, which in turn can hold other arrays as well. In such a way you can create two-dimensional or three-dimensional array.

Create a Multi-Dimensional Array (Easy/Complex)

You can create arrays in PHP by creating a an array assigning it to a variable then putting said variable in arrays. This is the easiest way to create a muti-dimensional array:

$user1 = array("email" => "john@demo.com",
               "website" => "www.john.com",
               "age" => "22",
               "password" => "pass");
 
$user2 = array("email" => "anna@demo.com",
               "website" => "www.anna.com",
               "age" => "24",
               "password" => "pass");
 
$user3 = array("email" => "peter@mail.com",
               "website" => "www.peter.com",
               "age" => "42",
               "password" => "pass");
 
$user4 = array("email" => "max@mail.com",
               "website" => "www.max.com",
               "age" => "33",
               "password" => "pass");
 
$userList = array("John" => $user1,
                  "Anna" => $user2,
                  "Peter" => $user3,
                  "Max" => $user4);

As you can see an array is created for each user including their personal details. These are assigned to a variable, when another array is created the user details arrays are then assigned to their name in an array. Done, You have created a multi-dimensional array.

The correct method of creating The Same multi-dimensional array is as follows:

$userList = array("John" => array(
                     "email" => "john@demo.com",
                     "website" => "www.john.com",
                     "age" => "22",
                     "password" => "pass"),
                  "Anna" => array(
                     "email" => "anna@demo.com",
                     "website" => "www.anna.com",
                     "age" => "24",
                     "password" => "pass"),
                  "Peter" => array(
                     "email" => "peter@mail.com",
                     "website" => "www.peter.com",
                     "age" => "42",
                     "password" => "pass"),
                  "Max" => array(
                     "email" => "max@mail.com",
                     "website" => "www.max.com",
                     "age" => "33",
                     "password" => "pass")
                   );

Multi-Dimensional Iteration

Its just like normal array iteration except
you have to dive a bit deeper here is an example:

echo "John is ".$userList["John"]["age"]." years old.";

As you can see, if we know the name John then we can output his personal information. If you wish to output the data for a unknown persons you can use foreach loops:

foreach ($userList as $key=>$value) {
   echo "The actual user is $key.<br/>";
   foreach ($value as $iKey => $iValue) {
      echo " ---> $iKey - $iValue <br/>";
   }	
}

This will output every user name including their personal details.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis
Written by Adam in: 03. Arrays | Tags: , ,

No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a comment

WordPress Powered, Theme by TheBuckmaker.com | Add to Technorati Favorites. | RSS and Comments RSS