How to define a multidimensional array using PHP? (PHP Multidimensional array)

To better understand how to define a mutlidimensional array using PHP, lets have a glimpse on how do we define a single dimensional array in PHP. PHP multidimensional array

Single Dimensional Array in PHP


<?php
$vehicles=array("car","bus","truck","van","xuv");
foreach ($vehicles as $vehicle) {
echo $vehicle.'<br/>';
}
?>

output:

car
bus
truck
van
xuv

In the above example code, we have defined a single dimensional array in PHP. $vehicles array variable contains name of different vehicles.

Now suppose, we want to store vehicle names along with its price in an array, then how can we achieve this?

In such case we need multidimensional array.

Multidimensional Array


So lets see how can we store the vehicle names along with its price in an array variable.

<?php
$vehicles=array("car"=>20000,"bus"=>50000,"truck"=>70000,"van"=>20000,"xuv"=>45000);
foreach ($vehicles as $key=>$value) {
echo $key." ".$value;
echo '<br/>';
}
?>

Output:

car 20000
bus 50000
truck 70000
van 20000
xuv 45000


In the above example. we have declared a two dimensional array. The array variable $vehicles contains the vehicle name and its price. It is like key and value pair. So in PHP, in each loop we can print each array element as key and value.

In some case, we may need to print only the key. In this example, if we want to print the vehicles name only then we need to print the $key only. Thats it.

<?php
$vehicles=array("car"=>20000,"bus"=>50000,"truck"=>70000,"van"=>20000,"xuv"=>45000);
foreach ($vehicles as $key=>$value) {
echo $key;
echo '<br/>';
}
?>

Output

car
bus
truck
van
xuv


Similarly if we want only price then

<?php
$vehicles=array("car"=>20000,"bus"=>50000,"truck"=>70000,"van"=>20000,"xuv"=>45000);
foreach ($vehicles as $key=>$value) {
echo $value;
echo '<br/>';
}
?>

output:
20000
50000
70000
20000
45000


Now, if we want to store vehicle name, its price and the year of manufacturing then how could we store in an array variable?

Lets see how can we store vehicle name, price and manufacture year for car only in an array.

For this, we need an array that holds data in a key and value format.

<?php
$vehicles=array("vehicle"=>"car","price"=>20000,"manufacture_year"=>1995);
?>

Now if we want to store more than one array element in $vehicles, then we need to keep all the array elements as children of an array object.

<?php
$vehicles=array(array("vehicle"=>"car","price"=>20000,"manufacture_year"=>1995),
array("vehicle"=>"bus","price"=>50000,"manufacture_year"=>1990),
array("vehicle"=>"truck","price"=>70000,"manufacture_year"=>2000),
array("vehicle"=>"van","price"=>20000,"manufacture_year"=>2005),
array("vehicle"=>"xuv","price"=>45000,"manufacture_year"=>2010));
foreach ($vehicles as $vehicle) {
echo $vehicle["vehicle"]." ".$vehicle["price"]." ".$vehicle["manufacture_year"];
echo '<br/>';
}
?>

Output:

car 20000 1995
bus 50000 1990
truck 70000 2000
van 20000 2005
xuv 45000 2010


In the above example, all the array elements are the children of a array object.

Note: Each array inside the array are separated with a comma.
How to define a multidimensional array using PHP? (PHP Multidimensional array) How to define a multidimensional array using PHP? (PHP Multidimensional array) Reviewed by Pixel Stream on December 09, 2019 Rating: 5

No comments:

Powered by Blogger.