可能重复: php-如何打印此多维数组?
Possible Duplicate: php - How do I print this multidimensional array?
我有一个以下格式的数组
I have an array in below format
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 that array as below format
Product ID Amount 33 1 34 3 10 1任何人都可以在这个问题上帮助我.变量的var_dump是.
Can anyone please help me regarding this problem. var_dump of the 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)推荐答案
我相信这是您的数组
$array = Array ( 0 => Array ( "product_id" => 33 , "amount" => 1 ) , 1 => Array ( "product_id" => 34 , "amount" => 3 ) , 2 => Array ( "product_id" => 10 , "amount" => 1 ) );使用foreach
echo "<pre>"; echo "Product ID\tAmount"; foreach ( $array as $var ) { echo "\n", $var['product_id'], "\t\t", $var['amount']; }使用array_map
echo "<pre>" ; echo "Product ID\tAmount"; array_map(function ($var) { echo "\n", $var['product_id'], "\t\t", $var['amount']; }, $array);输出
Product ID Amount 33 1 34 3 10 1