Multidimensional Array

The multidimensional array is an array in which each element can also be an array and each element in the sub-array can be an array or further contain array within itself and so on. Values in the multi-dimensional array are accessed using multiple index. For example :
 $array_name = array(
   array(
     key1 => value1,
     key2 => value2
     keyN => valueN
   ),
   array(
     key1 => value1,
     key2 => value2
     keyN => valueN
   ),
   array(
     key1 => value1,
     key2 => value2
     keyN => valueN
   )
 );
Example :
<?php

  $employees = array(
    array(
      "Name" => "John snow",
      "Age" => "35",
      "Salary" => "20000"      
    ),
    array(
      "Name" => "Luci hunt",
      "Age" => "24",
      "Salary" => "15000"
    ),
    array(
      "Name" => "James bond",
      "Age" => "55",
      "Salary" => "75000"
    )
  );

  echo "Employee details : <br/><br/>";

  echo "Name : " . $employees[0]["Name"] . "<br/>";
  echo "Age : " . $employees[0]["Age"] . "<br/>";
  echo "Salary : " . $employees[0]["Salary"] . "<br/><br/>";

  echo "Name : " . $employees[1]["Name"] . "<br/>";
  echo "Age : " . $employees[1]["Age"] . "<br/>";
  echo "Salary : " . $employees[1]["Salary"] . "<br/><br/>";

  echo "Name : " . $employees[2]["Name"] . "<br/>";
  echo "Age : " . $employees[2]["Age"] . "<br/>";
  echo "Salary : " . $employees[2]["Salary"] . "<br/><br/>";
?>
Output :

Employee details :

Name : John snow
Age : 35
Salary : 20000

Name : Luci hunt
Age : 24
Salary : 15000

Name : James bond
Age : 55
Salary : 75000

The count() function :

The count() function is used to return the length or the number of elements of an array. for example :
<?php

  $employees = array(
    array(
      "Name" => "John snow",
      "Age" => "35",
      "Salary" => "20000"      
    ),
    array(
      "Name" => "Luci hunt",
      "Age" => "24",
      "Salary" => "15000"
    ),
    array(
      "Name" => "James bond",
      "Age" => "55",
      "Salary" => "75000"
    )
  );

  echo count($employees);

?>
Output :

3

Loop Through the Multidimensional Array :

By using the foreach loop we can get all the values of an multidimensional array. For example :
<?php

  $employees = array(
    array(
      "Name" => "John snow",
      "Age" => "35",
      "Salary" => "20000"      
    ),
    array(
      "Name" => "Luci hunt",
      "Age" => "24",
      "Salary" => "15000"
    ),
    array(
      "Name" => "James bond",
      "Age" => "55",
      "Salary" => "75000"
    )
  );

  $length = count($employees);

  for($x = 0; $x < $length; $x++) {

    foreach($employees[$x] as $x_key => $x_value) {
      echo $x_key . " = " . $x_value . "<br/>";
    }
    echo "<br/>";

  }

?>
Output :

Name = John snow
Age = 35
Salary = 20000

Name = Luci hunt
Age = 24
Salary = 15000

Name = James bond
Age = 55
Salary = 75000


Next Topic :