What is a Multidimensional Array in PHP?

What is a Multidimensional Array in PHP?

  • PHP
  • 3 mins read

A multidimensional array in PHP is an array of one or more arrays. In this array, each element of the main array can also be an array and each element in the sub-array can be an array. To access the value of an array element in the multidimensional array we use multiple indices.

About Multidimensional Array in PHP

What is a Two-Dimensional Array in PHP?

A two-dimensional array is also an array of sub-arrays. In two dimensional array in PHP, when we want to access the value of an array element, we will use two indices(one is a row, and another is a column).

How to Create a Two Dimensional Indexed Array in PHP?

A two-dimensional array can be created using the predefined function array(). Here is an example of creating a two-dimensional array:

$a=array(array("rabi","class-v","marks-90"),array("amit","class-v1","marks-91"),array("asit","class-vii","marks-98"))

Here the two-dimensional array is containing three sub-arrays. The first sub-array is containing three elements, the second sub-array is also containing three elements, and a third sub-array is also containing three elements.

Here is an example for accessing each array element of a two dimensional array:

$a=array(array("amit","habra","67000"),array("gargi","kolkata","87000"),array("asit","barasat","98000"));

Here $a[0][0]=1st array element value="amit".

$a[0][1]=2nd array element value="habra"
$a[0][2]=3rd array element value="67000"
$a[1][0]=4th array element value="gargi"
$a[1][1]=5th array element value="kolkata"
$a[1][2]=6th array element value="87000"
$a[2][0]=7th array element value="asit"
$a[2][1]=8th array eleme nt value="barasat"
$a[2][2]=9th array element value="98000"

PHP Program to Display The Value of Each Array Element of a two-dimensional Array

For the above program, we will create a two-dimensional array by using some sub-arrays with array() function, where we will access the array elements with two indices(i.e., one is a row, and another is a column). Here we will use two loops; one is an inner loop and second is an outer loop, an inner loop will go through each column, and the outer loop will go through each row.

<html>
    <body>
        <?php
echo "<b>The output is given below:-</b>"."<br>";
$a=array(array("computer","24","35000"),array("computer","30","55000"),array("computer","14","15000"));//here $a is containing three sub arrays
for($i=0;$i<3;$i=$i+1)
{
    for($j=0;$j<3;$j=$j+1)
    {
        echo $a[$i][$j]." ";
    }
    echo "<br>";
}
        ?>
    </body>
</html>

Output: The output is given below:-

computer 24 35000 
computer 30 55000 
computer 14 15000

Here for each running of the outer loop, the inner loop will run for three times, and the value of each array element will be displayed with a space using here echo $a[$i][$j]." " statement. After displaying the value of array elements of each row, a new line is creating here by an echo "<br>" statement.