PHP Multi-dimensional Arrays Tutorial
In this tutorial we learn about arrays inside of arrays, called multi-dimensional arrays.
We cover how to create these nested arrays, and how to access them with either the indexer or with loops.
What are multi-dimensional arrays?
We know that an array is a collection, or list, of key/value pairs. What if we needed some of those values, to have multiple values of their own?
PHP provides us with a way of creating arrays inside arrays, which is referred to as multi-dimensional arrays.
How to define a multi-dimensional array
To create a multi-dimensional array, we simply define an array inside an element of another array.
<?php
// This array will be placed inside another
$meats = ["Steak", "Pork", "Chicken"];
// Main array
$shopping = ["Milk", "Bread", "Cookies", $meats];
?>
In the example above, we define the two arrays separately, however, we can define an array directly inside the main array we want to place it in.
<?php
// Main array
$shopping = ["Milk", "Bread", "Cookies", ["Steak", "Pork", "Chicken"]];
?>
How to access elements in a multi-dimensional array with the indexer
We can access the items in a multi-dimensional array with a double indexer. The first indexer is used to access the index where the inner array resides and the second index is to access the value of that inner array.
Inner array element 0 | Inner array element 1 | Inner array element 2 | |
---|---|---|---|
Main array element 0 | x[0][0] | x[0][1] | x[0][2] |
Main array element 1 | x[1][0] | x[1][1] | x[1][2] |
Main array element 2 | x[2][0] | x[2][1] | x[2][2] |
$arrayName[outerArrayIndex][innerArrayIndex];
<?php
$shopping = [
// Main array elements
"Milk", # 0
"Bread", # 1
"Cookies", # 2
// Inner array elements
[
"Steak", # 3.0
"Pork", # 3.1
"Chicken" # 3.2
]
];
echo $shopping[3][2];
?>
In the example above, we access the main array’s fourth element with the first indexer $shopping[3].
The fourth element is the inner array, because we access it with the first indexer, the interpreter now has the ability to go inside the inner array, and access the value from the element we specify in the second indexer, $shopping[3][2].
When we want to access an element that is not an inner array, we simply specify only one indexer.
<?php
$shopping = [
// Main array elements
"Milk", # 0
"Bread", # 1
"Cookies", # 2
// Inner array elements
[
"Steak", # 3.0
"Pork", # 3.1
"Chicken" # 3.2
]
];
// Outer array access
echo $shopping[0] . "<br>";
// Inner array access
echo $shopping[3][2];
?>
In the example above, we use a single indexer to access an element that is not an array itself.
A multi-dimensional array where only one element has an inner array, is impractical. Multi-dimensional arrays are typically used when all elements are inner arrays.
<?php
$shopping = [
// Toiletries
["Soap", "Shampoo", "Shaving cream"],
// Condiments
["Mayonaise", "Ketchup", "Mustard"],
// Daily
["Milk", "Bread", "Eggs"],
// Meats
["Steak", "Pork", "Chicken"]
];
echo "Toiletries: ";
echo $shopping[0][0] . ", ";
echo $shopping[0][1] . ", ";
echo $shopping[0][2] . "<br>";
echo "Condiments: ";
echo $shopping[1][0] . ", ";
echo $shopping[1][1] . ", ";
echo $shopping[1][2] . "<br>";
echo "Daily: ";
echo $shopping[2][0] . ", ";
echo $shopping[2][1] . ", ";
echo $shopping[2][2] . "<br>";
echo "Meats: ";
echo $shopping[3][0] . ", ";
echo $shopping[3][1] . ", ";
echo $shopping[3][2] . "<br>";
?>
In the example above, all the elements in the $shopping array, are arrays themselves.
How to access elements in a multi-dimensional array with a loop
Similar to normal arrays, we can access multi-dimensional array values by using a foreach loop.
In this case however, we must loop twice. Once, to loop over all the inner arrays that are inside the main array, and another loop to loop over each element of each inner array.
foreach($mainArray as $tempInnerArray) {
foreach($tempInnerArray as $innerArrayElement) {
// use $innerArrayElement
}
}
<?php
$shopping = [
// Toiletries
["Soap", "Shampoo", "Shaving cream"],
// Condiments
["Mayonaise", "Ketchup", "Mustard"],
// Daily
["Milk", "Bread", "Eggs"],
// Meats
["Steak", "Pork", "Chicken"]
];
foreach($shopping as $innerArray) {
// Secondary loop for inner array elements
foreach($innerArray as $item) {
echo $item . ", ";
}
echo "<br>";
}
?>
In the example above we loop over the $shopping array first. As the interpreter accesses each inner array, it is told to loop through that inner array’s elements and print each item to the page.
Summary: Points to remember
- A multi-dimensional array is an array inside an array.
- Multi-dimensional arrays elements are accessed with extra indexers or nested loops.