How to Print Multidimensional Array in PHP

In this article, you going to see the discussion regarding how to Print Multidimensional Array in PHP in a detail and step by step.

If you are looking for this topic then you are at a right place.

Let’s start with the topic to Print Multidimensional Array in PHP.

Consider below example of array.

Array ( [0] => Array ( [vehicle_id] => 33 [amount] => 1 ) [1] => Array ( [vehicle_id] => 34 [amount] => 3 ) [2] => Array ( [vehicle_id] => 10 [amount] => 1 ) )   

or

$array = Array ( 
        0 => Array ( "vehicle_id" => 33 , "amount" => 1 ) ,
        1 => Array ( "vehicle_id" => 34  , "amount" => 3 ) ,
        2 => Array ( "vehicle_id" => 10  , "amount" => 1 ) );

Now our next step is to print this array.

There are two method to print a multidimensional array.

One is using array map and the another one using the foreach function.

Before going to discussion regarding how to print multidimensional array.

Discuss first point PHP echo Multidimensional Array.

How the function called array_map() function is used to print multidimensional array.

Array map fuction is used to send the each and every value of an array to self made function.

And then, it returns an array with new value, which is given by self made function.

Syntax for the function called array_map() function is given below.

array_map(varfunction, array1, array2, array2,...)

Below is detail information regarding syntax.

ParameterDetails
varfunctionIt is Required. The name of the self-made function, or null
array1It is Required. Represent an array
array2It is Optional. Represent an array
array3It is Optional. Represent an array
Syntax Details

Below is the code snippet using array map function from which you can understand easily.

<h1>How to Print Multidimensional Array in PHP</h1>
<?php 
$varArray = Array ( 
        0 => Array ( "vehicle_id" => 33 , "amount" => 1 ) ,
        1 => Array ( "vehicle_id" => 34  , "amount" => 3 ) ,
        2 => Array ( "vehicle_id" => 10  , "amount" => 1 ) );
echo "\n";
echo "<h2>Using array map output</h2>"; 
		
function funCalculation($var) {
    echo "\n", $var['vehicle_id'], "\t\t", $var['amount'];
}

echo "<pre>" ;
echo "Vehicle ID\tAmount";
array_map("funCalculation", $varArray);

?>
</div>

OUTPUT :

Using Array Map Function
Using Array Map Function

PHP Multidimensional Array Foreach

You can also use for-each function to print Multidimensional Array in PHP.

In the for-each you have to just pass the array.

It undergo looping and give the output as per your requirement.

Below is the code snippet

<div style="background-color:#e5bcbc;" align="center">
<h1>How to Print Multidimensional Array in PHP</h1>
<?php 
$varforArray = Array ( 
        0 => Array ( "emp_id" => 401 , "amount" => 100 ) ,
        1 => Array ( "emp_id" => 402  , "amount" => 200 ) ,
        2 => Array ( "emp_id" => 403 , "amount" => 300 ) );
		
echo "\n";
echo "<h2>Using foreach output</h2>";

echo "<pre>";
echo "Employ ID\tAmount";
foreach ($varforArray as $var) {
    echo "\n", $var['emp_id'], "\t\t", $var['amount'];
}

?>
</div>

OUTPUT USING FOR-EACH GIVEN BELOW :

Using For-Each Function
Using For-Each Function

str_replace php : What is str_replace function in PHP

Conclusion :

Use the above code to do topic of Printing Multidimensional Array in PHP.

I hope you like this article and if you think that i have missing something, please comment below.

Leave a Comment