The PHP multidimensional array refers to arrays inside another arrays, which means the ability to design a big tabular by arrays.

For example.

<?php
  $array = array(
     array(
          '125,0501.00',
          '90,541.00',
          '1,150.00',
      ),
      array(
          'IT Department',
          'Graphic Department',
          'Programming Department',
      ),
      array(
          '3,000,000.00',
          '1,000,000.00',
          '2,500,000.00',
      )
  );
?>

The following image shows you an image contains tables which are designed to expose the data inside the above example.

php multidimensional array

As you see in the above image, It shows you the number of rows and columns that are already specified in the above PHP example.

Anyway, In the following section, I will focus on what does mean the dimensions in the array. Let’s move forward.

The Dimensions in the Array

Actually, dimensions are the number of the elements inside any array, which means directions inside the arrays that are allowed to be expanded in any direction.

The below diagram shows you an example.

php multidimensional array

So the main array has another arrays that can be expanded from any direction (left, right, top, bottom), such as the below table.

Tabular of multidimensional arrays

So, there is no limit for directions which means there is no limit for rows, columns, or arrays.

Accessing the PHP Multidimensional Array

Firstly, you have to know how many indexes there and how to access the right way to reach the right index. The following is a multidimensional array for a multiplication table.

<?php 

$array = array(
   array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ), // => 0
   array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ), // => 1
   array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ), // => 2
   array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ), // => 3
   array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ), // => 4,
   array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ), // => 5,
   array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ), // => 6,
   array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ), // => 7, 
   array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ), // => 8,
   array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ), // => 9, 
   array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ), // => 10, 
   array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ), // => 11, 
   array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ), // => 12
);

As you know the array starting from 0 index. To a void this issue in the multiplication table, I will add 1, so the final records is 5. Let’s see how to access the multidimensional array.

<?php 

for( $x = 0; $x < count( $array ) ; $x++ ) {
  $basic_number = ( $x + 1 );
  for( $y= 0; $y < count( $array[$x] ); $y++ ) {
    $print = $basic_number . " x " . $array[$x][$y] . " = " . ( $basic_number * $array[$x][$y] );
    echo $print;
    echo "\n";
  } 
}

So the output would be the multiplication table from 1 to 12.

Anyway, it is so easy to accessing a multidimensional array, you just need to know the type of the array if it is an indexed array. So you will reach it using the numerical values that started from 0 with square brackets. For example.

$array = [ array( 1,2 ), 1, true, -50 ];
$array[0][1] // 2 => the second value of the second array

PHP Multidimensional Array Examples

The following is an example for the indexed array type with a multidimensional.

<?php
   $mul = [
     [  // => index: 0
       50, 
       23, 
       54, 
       "Value 1" // => index: 3
     ], 
     [
       "item 1", 
       "item 2", 
       "item 3", 
       "item 4"
     ],
     [ // => index: 2
       true, 
       false, 
       "CodedTag",  // => index: 2
       -20
     ],
   ]; 

   echo $mul[0][3] // Value 1
   echo $mul[2][2] // CodedTag
?>

Using the multidimensional array with associative arrays.

<?php
   $asc_array = array(
      "salaries" => array( 
         array(
            "name"   => "Ahmed",  
            "salary" => "500,000.00"
         ),
         array(
            "name" => "Montaser",
            "salary" => "100,000.00"
         )
      ),
      "period" => array(
        array(
           "from" => "01-01-2012",
           "to"   => "31-12-2012",
        ),
        array(
           "from" => "01-01-2016",
           "to"   => "31-12-2016",
        )
      )
   );
?>

Wrapping Up

The PHP multidimensional array is expandable arrays inside arrays which can takes unlimited elements with unlimited arrays.