What is a multidimensional array?
In the PHP Arrays lesson, you’ve learnt that each key/index of an array can contain only one value – a number or a string. Well, instead of a single value, a key may contain another array and then the array is called a multidimensional array.
How to create a multidimensional array
Suppose, you want to make a list with the names of your friends. To do this, you can make an array(ex. $friends) which will store names of all your friends. (see the following example). Here, $friends is an example of a simple an array or a one-dimensional array.
Example (one–dimensional array)
<?php $friends = array(“John”, “Jack”, “Smith”); ?>
The following is the tabular representation of the above one dimensional array-
keys | 0 | 1 | 2 |
values | John | Jack | Smith |
In the table, the first row is the keys of the array which is auto generated and the second row is the values of the corresponding keys which are the names of the friends.
Now, instead of just storing the name of each friends, you also want to store location, age, and profession of each person. How you’ll do it? Well, you can declare an array in place of each friend name in the above array and each of these new internal arrays will store location, age, and profession of each friend. The following example will show you what I mean-
Example (two dimensional array)
<?php $friends = array( “John”=>array(“location” => “US”, “age” => 29, “profession” => “Designer”), “Jack”=>array(“location” => “UK”, “age” => 34, “profession” => “Marketer”), “Smith”=>array(“location” => “Mexico”, “age” => 25, “profession” => “Banker”) ); ?>
As you can see in the above example, each internal/nested array holds location, age, and profession of one person. To identify who own the information of an internal array, we used the friend-name as the key, ex. the information in the first internal array is for John.
Now, the $friends array above is a two-dimensional array as it has one level of arrays inside the main array($friends). Each element of the $friends array is another array and each of these arrays consists of location, age, and profession of one person.
The following is the tabular representation of the above two dimensional array-
keys | John | Jack | Smith | ||||||
keys | location | age | profession | location | age | profession | location | age | profession |
values | US | 29 | Designer | UK | 34 | Marketer | Mexico | 25 | Banker |
Here, the first row is the keys of the outer most array which is $friend array. The second row which is the position of the values of the keys of the first rows. But, it consists of keys; so, these are the keys of the internal arrays and their corresponding values are represented by the third row.
Now, suppose, instead of just location, you want to store both home location and office location of each friend. So, you need to declare an array in the location position which will store two elements – home location and office location like the following example-
Example (three dimensional array)
<?php $friends = array( “John”=>array(“location” => array(“home” => “Eureka”, “office” => “Mendocino”), “age” => 29, “profession” => “Designer”), “Jack”=>array(“location” => array(“home” => “Oxford”, “office” => “London”), “age” => 34, “profession” => “Marketer”), “Smith”=>array(“location” => array(“home” => “Centro”, “office” => “Zokalo”), “age” => 25, “profession” => “Banker”) ); ?>
The following is the tabular representation of the above three dimensional array
keys | John | Jack | … | ||||||
keys | location | age | profession | location | age | profession | … | ||
keys | home | office | home | office | … | ||||
values | Eureka | Mendocino | 29 | Designer | Oxford | London | 34 | Marketer | … |
How to access a multidimensional array element?
In the following, at first you’ll see how to access a specific element in a multidimensional array, next, you’ll see how to access all the elements-
(a) Accessing a specific element in a multidimensional array
To access an element content of a multidimensional array, mention the name of the array then mention the keys in the bracket-pairs from the outer most array to the inner one upto that element.
For example, we’ll retrieve the profession of John from the two dimensional array in the following example. To do it, in line 8, at first, we mention the name of the array $friend, then in the first bracket-pair, we mention the key of the outer array which is [“John”], then, in the next bracket-pair, we mention the inner array key which is [“profession”].
<?php $friends = array( "John"=>array("location" => "US", "age" => 29, "profession" => " Designer"), "Jack"=>array("location" => "UK", "age" => 34, "profession" => "Marketer"), "Smith"=>array("location" => "Mexico", "age" => 25, "profession" => "Banker") ); echo $friends["John"]["profession"]; ?>
[wpdm_file id=170]
Output:
Designer
Please note that to access the above value of the above two dimensional array, we used two bracket pairs.
In the following, the shaded portion shows which keys are selected in order (from top to bottom) to display John’s profession.
keys | John | Jack | Smith | ||||||
keys | location | age | profession | location | age | profession | location | age | profession |
values | US | 29 | Designer | UK | 34 | Marketer | Mexico | 25 | Banker |
Similarly, to retrieve John’s office location from the following three dimensional $friends array, you can follow the code below-
<?php $friends = array( "John"=>array("location" => array("home" => "Eureka", "office" => "Mendocino"), "age" => 29, "profession" => "Designer"), "Jack"=>array("location" => array("home" => "Oxford", "office" => "London"), "age" => 34, "profession" => "Marketer"), "Smith"=>array("location" => array("home" => "Centro", "office" => "Zokalo"), "age" => 25, "profession" => "Banker") ); echo $friends["John"]["location"]["office"]; ?>
[wpdm_file id=171]
Output:
Mendocino
The shaded portion shows which keys are selected in order (from top to bottom) to display John’s office location-
keys | John | Jack | … | ||||||
keys | location | age | profession | location | age | profession | … | ||
keys | home | office | home | office | … | ||||
values | Eureka | Mendocino | 29 | Designer | Oxford | London | 34 | Marketer | … |
(b) Accessing all the elements in a multidimensional array
There are few ways to access all the elements in a multidimensional array. In the following, we’ll use foreach loops to access elements. Please note that to access all the elements of the following two-dimensional array, we used two foreach loops. The first foreach loop will traverse elements of the outer array, and the second array will traverse the elements of the inner array.
<?php $friends = array( "John"=>array("location" => "US", "age" => 29, "profession" => "Designer"), "Jack"=>array("location" => "UK", "age" => 34, "profession" => "Marketer"), "Smith"=>array("location" => "Mexico", "age" => 25, "profession" => "Banker") ); foreach($friends as $friend_name => $friend_detail){ echo "<u>"."Name: ". $friend_name."</u><br />"; foreach($friend_detail as $key => $value){ echo $key . ": " . $value." "; } echo "<br /><br />"; } ?>
[wpdm_file id=172]
Output:
Name: John
location: US age: 29 profession: Designer
Name: Jack
location: UK age: 34 profession: Marketer
Name: Smith
location: Mexico age: 25 profession: Banker
Similarly, to access elements of a three dimensional array, you’ll use three foreach loops and so on.
How to change a multidimensional array element?
To change the value of a multidimensional array element, mention the element in the left side of the equal sign and the new value at right side of it. Look at the following example-
Syntax
$array_name[outer_key]…[inner_key] = “new_value”;
Example: the following example will change the value Smith’s new office to Allende
<?php $friends = array( "John"=>array("location" => array("home" => "Eureka", "office" => " Mendocino"), "age" => 29, "profession" => "Designer"), "Jack"=>array("location" => array("home" => "Oxford", "office" => "London"), "age" => 34, "profession" => "Marketer"), "Smith"=>array("location" => array("home" => "Centro", "office" => " Zokalo"), "age" => 25, "profession" => "Banker") ); $friends["Smith"]["location"]["office"] = “Allende”; ?>
How to delete a multidimensional array element?
(a) To delete a specific element of a multidimensional array, mention the element in the unset() function. The following example will delete Smith’s profession-
<pre> <?php $friends = array( "John"=>array("location" => "US", "age" => 29, "profession" => " Designer"), "Jack"=>array("location" => "UK", "age" => 34, "profession" => "Marketer"), "Smith"=>array("location" => "Mexico", "age" => 25, "profession" => "Banker") ); unset($friends["Smith"]["profession"]); echo print_r($friends); ?> </pre>
[wpdm_file id=173]
Output:
Array(
[John] => Array
(
[location] => US
[age] => 29
[profession] => Designer
)
[Jack] => Array
(
[location] => UK
[age] => 34
[profession] => Marketer
)
[Smith] => Array
(
[location] => Mexico
[age] => 25
)
(b) To delete an entire multidimensional array, simple mention the name of the array in the unset() function like the following-
<?php $friends = array( "John"=>array("location" => "US", "age" => 29, "profession" => " Designer"), "Jack"=>array("location" => "UK", "age" => 34, "profession" => "Marketer"), "Smith"=>array("location" => "Mexico", "age" => 25, "profession" => "Banker") ); unset($friends); ?>