PHP Multidimensional Array

PHP Multidimensional Array

PHP Multidimensional Array

PHP multidimensional array is also known as array of arrays. It allows you to store tabular data in an array. PHP multidimensional array can be represented in the form of matrix which is represented by row * column.


Definition

There are two ways to define associative array:

1st way :

$emp = array  
  (  
  array(1,"sonoo",400000),  
  array(2,"john",500000),  
  array(3,"rahul",300000)  
  ); 

PHP Multidimensional Array Example

Let's see a simple example of PHP multidimensional array to display following tabular data. In this example, we are displaying 3 rows and 3 columns.

ID Name Salary
1 sonoo 40000
2 John 50000
3 Rahul 40000

Example

<?php    
$emp = array  
  (  
  array(1,"sonoo",400000),  
  array(2,"john",500000),  
  array(3,"rahul",300000)  
  );  
  
for ($row = 0; $row < 3; $row++) {  
  for ($col = 0; $col < 3; $col++) {  
    echo $emp&#91;$row&#93;&#91;$col&#93;."  ";  
  }  
  echo "<br/>";  
}  
?>  
Output :

1. Sonoo : 400000
2. John : 500000
3. Rahul : 300000