PHP Multidimensional Array-with Keys and Indexes

The purpose of arrays is to store data in an organized manner and allow easy access using indexes or keys. You have learnt about one dimensional array in previous lesson. Extending the same array, imagine if you are able to store data in rows and columns. Storing different dimensions of data in an array is possible with multidimensional array. It can be seen as an array with in another array. In short a nested array is a multidimensional array.

Multiple dimensions of data cannot be represented in one dimensional array. So we need a nested array. Here is the syntax of multidimensional array-

$MultiDimenArray-Name=array( [key=>] array( [key=>]array…..)))

You can define an array as an element of an array. An array nested with in another array can be created with a string key like an associative array or without key. If no key is specified than index values are automatically allocated to array elements just like an indexed array.

Example of PHP Multidimensional Array

If you want to store data of different sales persons in different cities for different quarters of a year, PHP multidimensional array will be a handy tool.

Sales person: Andy

  First Quarter Second Quarter Third Quarter Fourth Quarter
New York 36 23 98 88
Chicago 78 56 45 37
Denver 34 78 36 21

Sales person: Bob

  First Quarter Second Quarter Third Quarter Fourth Quarter
New York 22 67 71 98
Chicago 76 53 83 61
Denver 45 69 23 45

Sales person: Chris

  First Quarter Second Quarter Third Quarter Fourth Quarter
New York 22 67 56 45
Chicago 76 53 78 36
Denver 22 67 71 98

Multidimensional Array with Keys

In the following example the nested arrays elements are associated with keys. So, while accessing them the keys must be used just like you did in the associative array.

<?php
$SalesData=array("Andy"=>
	array("NewYork"=>array("Q1"=>36,"Q2"=>23,"Q3"=>98,"Q4"=>88),
	      "Chicago"=>array("Q1"=>78,"Q2"=>56,"Q3"=>45,"Q4"=>37),
	      "Denver"=>array("Q1"=>34,"Q2"=>78,"Q3"=>36,"Q4"=>21)),
		"Bob"=>
		array("NewYork"=>array("Q1"=>22,"Q2"=>67,"Q3"=>71,"Q4"=>98),
		"Chicago"=>array("Q1"=>76,"Q2"=>53,"Q3"=>83,"Q4"=>61),
		"Denver"=>array("Q1"=>45,"Q2"=>69,"Q3"=>23,"Q4"=>46)	),
		"Chris"=>array( NewYork"=>array("Q1"=>22,"Q2"=>67,"Q3"=>56,"Q4"=>45),
		"Chicago"=>array("Q1"=>76,"Q2"=>53,"Q3"=>78,"Q4"=>36),
		"Denver"=>array("Q1"=>22,"Q2"=>67,"Q3"=>71,"Q4"=>98)));
				
echo $SalesData["Andy"]["Chicago"]["Q3"]. ',';	
echo $SalesData["Andy"]["NewYork"]["Q4"].",";	
echo $SalesData["Bob"]["Denver"]["Q1"].",";	
echo $SalesData["Bob"]["Chicago"]["Q2"].",";	
echo $SalesData["Chris"]["Denver"]["Q1"].",";	
echo $SalesData["Chris"]["NewYork"]["Q4"];
?>
PHP multidimensional array using keys

Multidimensional Array with Index

When you don’t specify keys while creating PHP multidimensional array, the indexes are automatically assigned to the elements. Now you can use indexes in the same way as you did with one dimensional array. You can use For Loop/While Loop for iterating through the nested array and use the elements as shown in the example code below.

<?php
$SalesData=array(array(array(36,23,98,88),array(78,56,45,37),array(34,78,36,21)),
			array(array(22,67,71,98),array(76,53,83,61),array(45,69,23,46)),
			array(array(22,67,56,45),array(76,53,78,36),array(22,67,71,98))
			);
				

for ($a1=0;$a1< count($SalesData); $a1++)
{
		
		for ($a2=0;$a2< count($SalesData[$a1]); $a2++) 
		{
			
				for ($a3=0;$a3<count($SalesData[$a1][$a2]);$a3++)
				{					
					echo $SalesData[$a1][$a2][$a3]." ";
				}
				echo "<br>";
		}
		echo "<br>";
}
?>
PHP multidimensional array using index