我有一个以下格式的数组
Array ( [0] => Array ( [product_id] => 33 [amount] => 1 ) [1] => Array ( [product_id] => 34 [amount] => 3 ) [2] => Array ( [product_id] => 10 [amount] => 1 ) )
我想从该数组获取输出,格式如下
Product ID Amount
33 1
34 3
10 1
任何人都可以帮助我解决这个问题。变量的var_dump是。
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 ) );
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)