PHP multidimensional array - Creating the multi array


Home - Tutorials - Basics

In this smal tutorial I will show you how to create and manage multidimensional arrays in PHP.

Tutorial info:


Name:PHP multidimensional array
Total steps:3
Category:Basics
Level:Beginner

Bookmark PHP multidimensional array



Bookmark and Share

Step 2 - Creating the multi array


PHP multidimensional array


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.  
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.  
Now it is time to work with our new array and try to print it in the next step.





Previous Step of PHP multidimensional arrayNext Step of PHP multidimensional array


Tags: php multidimensional array, multidimensional array, array, multi-dimensional array



Follow phpf1 on Twitter




F1 Site Family
AJAX F1
HTML F1
Java F1
JavaScript F1
PHP F1

Total time: 0.0279