Saturday, August 16, 2008

PHP multidimensional array

As you know arrays are a list of key and value pairs. In general the key used to be a number ( or a string in case of associative arrays ) and the value is any data type. And that is important as it means that a value can be an other array as well. So if you create an aeeay where the values are arrays as well then you made a multidimensional array. Of course the values in this internal array can be again a new array and so on.
Let's see an example. Suppose you want to store user informations. In this case you have a lot of users and each user has more properties like name, age, email, website and so on.

  • John
    • email : john@demo.com
    • website : www.john.com
    • age : 22
    • password : pass
  • Anna
    • email : anna@demo.com
    • website : www.anna.com
    • age : 24
    • password : pass
  • Peter
    • email : peter@mail.com
    • website : www.peter.com
    • age : 42
    • password : pass
  • Max
    • email : max@mail.com
    • website : www.max.com
    • age : 33
    • password : pass

 

Now let's see how to define such an array in PHP. You can do it in some smaller parts and in this case first you define the sub arrays and then add it to the main array as follows:

Code:

  1. $user1 = array("email" => "john@demo.com",

  2. "website" => "www.john.com",

  3. "age" => "22",

  4. "password" => "pass");

  5.  

  6. $user2 = array("email" => "anna@demo.com",

  7. "website" => "www.anna.com",

  8. "age" => "24",

  9. "password" => "pass");

  10.  

  11. $user3 = array("email" => "peter@mail.com",

  12. "website" => "www.peter.com",

  13. "age" => "42",

  14. "password" => "pass");

  15.  

  16. $user4 = array("email" => "max@mail.com",

  17. "website" => "www.max.com",

  18. "age" => "33",

  19. "password" => "pass");

  20.  

  21. $userList = array("John" => $user1,

  22. "Anna" => $user2,

  23. "Peter" => $user3,

  24. "Max" => $user4);

  25.  

PHP F1



Of course you can do all of it in only one step like this:



Code:

  1. $userList = array("John" => array(

  2. "email" => "john@demo.com",

  3. "website" => "www.john.com",

  4. "age" => "22",

  5. "password" => "pass"),

  6. "Anna" => array(

  7. "email" => "anna@demo.com",

  8. "website" => "www.anna.com",

  9. "age" => "24",

  10. "password" => "pass"),

  11. "Peter" => array(

  12. "email" => "peter@mail.com",

  13. "website" => "www.peter.com",

  14. "age" => "42",

  15. "password" => "pass"),

  16. "Max" => array(

  17. "email" => "max@mail.com",

  18. "website" => "www.max.com",

  19. "age" => "33",

  20. "password" => "pass")

  21. );

  22.  

PHP F1



 



Displaying an element from a multidimensional array is not so complicated.  You just need to define more keys when you want to access a given array value. You can do it like this:



Code:

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

PHP F1



However in some cases it would be fine to print all information about John. You can do this as follows:



Code:

  1. foreach ($userList["John"] as $key=>$value) {

  2. echo "John $key - $value <br/>";

  3. }

  4.  

PHP F1



And the last variation is if you want to display all information from the array. In this case it's the best to create nested loops like this:



Code:

  1. foreach ($userList as $key=>$value) {

  2. echo "The actual user is $key.<br/>";

  3. foreach ($value as $iKey => $iValue) {

  4. echo " ---> $iKey - $iValue <br/>";

  5. }

  6. }

  7.  

PHP F1

No comments: