Multidimensional Arrays


A Multidimantional Array is an Array Containing One or More Array in Single Array


Sometimes you want to store values with more than one key.

This can be stored in multidimensional arrays.


PHP - Two-dimensional Arrays A two-dimensional array is an array of arrays (a three-dimensional array is an array of arrays of arrays).

First, take a look at the following table:
Name In Stock Sold
Maruti 20 10
Tata 15 6
Hyundai 7 2
BMW 22 20

We can store the data from the table above in a two-dimensional array, like this:

$store = array(
"Maruti"=> array("InStock"=>20,"Sold"=>10),
"Tata"=> array("InStock"=>15,"Sold"=>6),
"Hyundai"=> array("InStock"=>7,"Sold"=>2),
"BMW"=> array("InStock"=>22,"Sold"=>20));

Example

<table border="1">
<tr>
<th>Car Name </th>
<th>In Stock </th>
<th>Sold </th>
</tr>
<?php
$car = array("Maruti"=> array("InStock"=>20,"Sold"=>10),
"Tata"=> array("InStock"=>15,"Sold"=>6),
"Hyundai"=> array("InStock"=>7,"Sold"=>2),
"BMW"=> array("InStock"=>22,"Sold"=>20));
foreach($car as $key=>$value)
{
?>
<tr>
<td><?php echo $key ;?> </td>
<td ><?php echo $value["InStock"];?> </td>
<td> <?php echo $value["Sold"];?></td>
</tr>
<?php
}
?>
</table>

Run Example>>