Multidimensional Array in PHP

PHP  multidimensional array is that array which contains one or more arrays as its element. A simple example of a PHP multidimensional array is given below…

$a = array(‘name’=>”Sayeed”, ‘language’=>array(‘PHP’,’HTML’,’CSS’), ‘status’=true);

The array variable $a contains another array as its element. So, the array $a is a multidimensional array.

Multidimensional array in PHP

Multidimensional array in PHP

How to print multidimensional array elements

There are various ways to print a PHP multidimensional array. Among them, two easiest ways are explained below…

Print array element using multilevel index

This method is used to print an individual element from a multidimensional array. See below example…

$a = array(1,array(1,2,3,4,5,array(100,200,300)));

Now, if you want to print 200, you have to print $a[1][5][1].

Here,

a[1] represents array(1,2,3,4,5,array(100,200,300)).

a[1][5] represents array(100,200,300) and

a[1][5][1] represents 200 elements.

So, you are now able to print any element from a multidimensional array using the above system. The PHP code to retrieve an element from a multidimensional array is shown below…


<?php 
 $a = array(1,array(1,2,3,4,5,array(100,200,300))); 
 echo "Desired element is ".$a[1][5][1];
?>

Output

Desired element is 200

Print array element using foreach loop statement

The elements of a multidimensional array can easily be retrieved using foreach loop statement. But the number of foreach loop will be the equal number of nested array. See below example…


<?php 

$a = array('first'=>array('second'=>array(1,2,3,4,5)));

echo "The multidimensional array elements are: ";

foreach($a as $b){

 foreach($b as $c){

  foreach($c as $value){

    echo $value." ";

  }

 }

}

?>

Output

The multidimensional array elements are: 1 2 3 4 5

The above multidimensional array variable $a contains three arrays. So, you have to use three foreach loop statements to retrieve the elements of this multidimensional array. But there is a limitation of this method. If the first array contains second element that is not an array, PHP compiler will show an error message. See below example…


<?php $a = array('first'=>array('second'=>array(1,2,3,4,5)),'name'=>"Sayeed");

echo "The multidimensional array elements are: ";

foreach($a as $b){

 foreach($b as $c){

  foreach($c as $value){

   echo $value." ";

  }

 }

}

?>

Output

The multidimensional array elements are: 1 2 3 4 5
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\php\index.php on line 9

Here, the second element of first array is not an array. So, a warning message is shown. You can easily solve this problem using is_array() built-in function. This function returns true if the argument is an array otherwise returns false. So, the above example can be written like below…


<?php 

$a = array('first'=>array('second'=>array(1,2,3,4,5)),'name'=>"Sayeed");

echo "The multidimensional array elements are: ";

foreach($a as $b){

 if(is_array($b)){

  foreach($b as $c){

   if(is_array($c)){

    foreach($c as $value){

    echo $value." ";

   }

  }else{

   echo $c." ";

  }

 }

 }else{

  echo $b." ";

 }

}

?>

Output

The multidimensional array elements are: 1 2 3 4 5 Sayeed

Now, the warning message has gone and the elements of multidimensional array are showing properly.

Kinds of multidimensional array

Multidimensional array can be two dimensional, three dimensional or more dimensions. Generally it is easy to maintain two or three dimensional array. But if the dimensions are increased more than two or three, it will be so difficult to maintain that multidimensional array. So, this article will show only two dimensional array because it is mostly used multidimensional array in PHP.

Two dimensional array

The two dimensional array contains another array as its elements that means a two dimensional array is an array of array. See a two dimensional table of an electronics shop…

Name Price Quantity
Mobile $100 7
Laptop $300 10
TV $1000 5

A one dimensional array is enough to represent only two columns of any table but when columns are increased more than two, one dimensional array is unable to represent the table. For this, you have to use two dimensional array if any table contains more than two columns. So, you must use two dimensional array to represent the above table. A two dimensional array to represent the above table is given below…


<?php 
$eshop = array( array("Mobile","$100",7), array("Laptop","$300",10), array("TV","$1000",5) ); 
?>

Looping in two dimensional array

PHP foreach loop statement can be easily used to retrieve the elements of a two dimensional array. An example is given below…


<?php

$eshop = array(
                array("Mobile","$100",7),
                array("Laptop","$300",10),
                array("TV","$1000",5)
              );

echo "<table>";
echo "<tr>";
echo "<th>Name</th> <th> Price </th><th>Quantity</th>";
echo "</tr>";
foreach($eshop as $row){
 echo "<tr>";
 foreach($row as $column){
  echo "<td>".$column."</td>";
 }
 echo "</tr>";
}
echo "</table>";

?>

Output

Name Price Quantity
Mobile $100 7
Laptop $300 10
TV $1000 5

PHP multidimensional array has been explained in this article from very beginning. If you face any problem to understand multidimensional array, feel free to contact with me from Contact us page. I’ll try my best to stay with you. Some important array functions will be discussed in next articles. Spend some time to study those articles and learn more about PHP important array functions.

No Responses

Leave a Comment

Your email address will not be published. Required fields are marked *