How to print multidimensional arrays in php

I believe this is your array

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

Using foreach

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

Using array_map

echo "<pre>" ;
echo "Product ID\tAmount";
array_map(function ($var) {
    echo "\n", $var['product_id'], "\t\t", $var['amount'];
}, $array);

Output

Product ID  Amount
33          1
34          3
10          1

Leave a Comment