我有一个多维数组,我需要总结具有相同键的值。
print_r($输入)
Array
(
[0] => Array
(
[id] => colors
[power] => Array
(
[green] => 12
[red] => 5
[orange] => 9
[black] => 6
[white] => 5
[blue] => 11
)
)
[1] => Array
(
[id] => colors
[power] => Array
(
[green] => 20
[red] => 40
[orange] => 80
[black] => 60
[white] => 100
[blue] => 110
)
)
[2] => Array
(
[id] => glossycolor
[power] => Array
(
[green] => 20
[red] => 40
[orange] => 80
[black] => 60
[white] => 100
[blue] => 110
)
)
)
我需要结果像
Array
(
[0] => Array
(
[id] => colors
[power] => Array
(
[green] => 32
[red] => 45
[orange] => 89
[black] => 66
[white] => 105
[blue] => 121
)
)
[1] => Array
(
[id] => glossycolor
[power] => Array
(
[green] => 20
[red] => 40
[orange] => 80
[black] => 60
[white] => 100
[blue] => 110
)
)
)
我尝试使用 array_shift 对值进行排序并对子数组值求和,但我失败了
$finalRate = array_shift($inputs);
foreach ($inputs as $val) {
foreach ($val as $key => $val) {
$finalRate[$key] += $val;
}
}
但失败并返回空数组。