我有这个数组:
Array
(
[0] => Array
(
[Banana] => Array
(
[BIL_RateNonTaxed] => 126
)
[Cherry] => Array
(
[BIL_RateNonTaxed] => 60
)
)
[1] => Array
(
[Cherry] => Array
(
[BIL_RateNonTaxed] => 105
)
)
)
我怎样才能得到这样的东西:
$Banana = Get all `BIL_RateNonTaxed` for `Banana` = 126
$Cherry = Get all `BIL_RateNonTaxed` for `Cherry` = 165
在问你之前我尝试过的是:
$Cherry = implode(',', array_map(function($el){ return $el['Cherry']['BIL_RateTaxed']; }, $datas));
$Banana = implode(',', array_map(function($el){ return $el['Banana']['BIL_RateTaxed']; }, $datas));
我的错误是什么?
非常感谢你的帮助。
答案 0 :(得分:0)
Not sure what you expect from the implode
but just replace it with array_sum
:
$Cherry = array_sum(array_map(function($el) {
return $el['Cherry']['BIL_RateTaxed'];
}, $datas));
答案 1 :(得分:0)
You could also try summing a nested array_column
.
$fruit = 'Banana';
$rateType = 'BIL_RateNonTaxed';
$banana = array_sum(array_column(array_column($datas, $fruit), $rateType));
This gives you a little more flexibility and will just return 0
if you use a fruit or rate type that isn't there.