如何在php中打印多维数组 [英] How to print multidimensional arrays in php
本文介绍了如何在php中打印多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
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
这篇关于如何在php中打印多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文