How to print multidimensional arrays in php

Possible duplicate:
php - How to print this multidimensional array?

I have an array in the format below

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

I want to get output from this array in the format below

  Product ID Amount
 33 1
 34 3
 10 1

Can someone help me in solving this problem. var_dump variable is.

  array
   0 => 
     array
       'product_id' => string '33' (length = 2)
       'amount' => string '1' (length = 1)
   1 => 
     array
       'product_id' => string '34' (length = 2)
       'amount' => string '3' (length = 1)
   2 => 
     array
       'product_id' => string '10' (length = 2)
       'amount' => string '1' (length = 1)

+4
source share
3 answers

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 
+4
source
  <table> <tr> <th>Product Id</th> <th>Ammount</th> </tr> <?php foreach ($yourArray as $subAray) { ?> <tr> <td><?php echo $subAray['product_id']; ?></td> <td><?php echo $subAray['amount']; ?></td> </tr> <?php } ?> </table> 
+3
source

Try it.

 foreach($arr as $a) { echo $a['product_id']; echo $a['amount']; } 

Format as per your requirement.

+1
source

Source: https://habr.com/ru/post/1436943/


All Articles