PHP multidimensional array - Print multidimensional 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 3 - Print multidimensional array


PHP multidimensional array



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.";
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.  
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.  





Previous 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.0393