PHP Multidimensional Arrays
A multidimensional array is an array which stores another array at each index rather than storing a single value. In simple words, a multidimensional array is an array of arrays.
In general practice, associative array are stored inside multidimensional arrays.
Creating a Multidimensional Array
As we have already seen how to create an associative array, and multidimensional array being an array of arrays, will hold the associative arrays inside it.
Syntax for creating multidimensional array:
<?php
/*
multidimensional array initialization
*/
$cars = array(
array(
"name"=>"Urus",
"type"=>"SUV",
"brand"=>"Lamborghini"
),
array(
"name"=>"Cayenne",
"type"=>"SUV",
"brand"=>"Porsche"
),
array(
"name"=>"Bentayga",
"type"=>"SUV",
"brand"=>"Bentley"
),
);
?>
As we are dealing with array inside an array here, hence to access any information, we first need to reach to that array, then get data from a particular array stored in the multidimensional array.
For example, if we want to display data for Urus car, then, first we have to get the first array inside our multidimensional array, which can be accessed using index 0, and then inside that array, we can display all the data, like we did in associative arrays.
<?php
/*
Accessing multidimensional array
*/
echo "Accessing multidimensional array...";
echo "Urus is an ".$cars[0]["type"]." manufactured by ".$cars[0]["brand"]."\n";
echo "Bentayga is an ".$cars[2]["type"]." manufactured by ".$cars[2]["brand"]."\n";
?>
Accessing multidimensional array...
Urus is an SUV manufactured by Lamborghini
Bentayga is an SUV manufactured by Bentley
Traversing PHP Multidimensional Array
Traversing an array means to iterate it starting from the first index till the last element of the array.
We can traverse a multidimensional array either using two for
loops or two foreach
or one for
loop and one foreach
. To know the syntax and basic usage of for
and foreach
loop, you can refer to the PHP for and foreach loop tutorial.
Using one for
loop and one foreach
In case of multidimensional array, we have to traverse the main array and then the arrays stored inside the main arrays, hence we need two loops.
While using the for
loop to traverse a multidimensional array we must know the size/length of the array, which can be found using the count()
function.
Let's take the array defined above for the first example of traversing multidimensional array.
<?php
/*
multidimensional array initialization
*/
$cars = array(
array(
"name"=>"Urus",
"type"=>"SUV",
"brand"=>"Lamborghini"
),
array(
"name"=>"Cayenne",
"type"=>"SUV",
"brand"=>"Porsche"
),
array(
"name"=>"Bentayga",
"type"=>"SUV",
"brand"=>"Bentley"
),
);
// array traversal
// find size of the array
$size = count($lamborghinis);
// using the for loop
for($i = 0; $i < $size; $i++)
{
foreach($cars[$i] as $key => $value) {
echo $key . " : " . $value . "\n";
}
echo "\n";
}
?>
name : Urus
type : SUV
brand : Lamborghini
name : Cayenne
type : SUV
brand : Prosche
name : Bentayga
type : SUV
brand : Bentley
In the above multidimensional array, we have the main array as indexed array and the arrays stored as elements are associative.
Bu the main array can also be associative, let's take an example for it.
Also, as the index in associative array is not numeric and not sequentially, hence to find the index values or keys(as data saved in associative array is in the form of key-value), we can use the function array_keys()
to get an array of the keys used in the associative array.
<?php
/*
multidimensional array initialization
*/
$cars = array(
"Urus" => array(
"type"=>"SUV",
"brand"=>"Lamborghini"
),
"Cayenne" => array(
"type"=>"SUV",
"brand"=>"Porsche"
),
"Bentayga" => array(
"type"=>"SUV",
"brand"=>"Bentley"
),
);
/*
array traversal
*/
// find size of the array
$size = count($lamborghinis);
// array keys
$keys = arra_keys($cars);
// using the for loop
for($i = 0; $i < $size; $i++)
{
echo $keys[$i]. "\n";
foreach($cars[$keys[$i]] as $key => $value) {
echo $key . " : " . $value . "\n";
}
echo "\n";
}
?>
Urus
type : SUV
brand : Lamborghini
Cayenne
type : SUV
brand : Prosche
Bentayga
type : SUV
brand : Bentley
Advantages of Multidimensional Array
Here are a few advantages of using multidimensional array in our program/script:
- Detailed information can be stored in multidimensional array.
- On top level, it can either be kept indexed or associative, which makes it more user-friendly, as they can use it as per their requirements.