A multidimensional array is an array that contains one or more arrays.
PHP supports multidimensional arrays that can be two, three, four, five, or more levels deep, although arrays deeper than three levels can be challenging to manage for most users.
The dimension of an array indicates how many indices are required to select an element.
|
A two-dimensional array is essentially an array of arrays (and a three-dimensional array is an array of arrays of arrays).
First, let’s examine the following table:
Name |
Stock |
Sold |
Volvo |
22 |
18 |
BMW |
15 |
13 |
Saab |
5 |
2 |
Land Rover |
17 |
15 |
We can store the data from the table above in a two-dimensional array as follows:
$cars |
The two-dimensional $cars
array now contains four arrays and has two indices: row and column.
To access the elements of the $cars
array, we need to specify both indices (row and column):
echo |
We can also use a for loop inside another for loop to access the elements of the $cars array (while still specifying both indices):
for |