在我的db之后的循环值之后,我得到了这样的数组输出:
Array
(
[0] => Array
(
[1] => 2
[2] => 3
[6] => 1
)
[1] => Array
(
[1] => 2
[6] => 2
)
.
.
.
more
)
我想要的是,哪些数组具有相同的键需要对它们的值求和:
前:
[0] => array (
[1] => 2
[6] => 1
)
[1] => array (
[1] => 2
[6] => 2
)
.
.
.
有两个关键是相同的
我希望得到这样的结果:
array (
[1] => 4 // after sum value
[2] => 3 // no duplicated key
[6] => 3 // after sum value
.
.
.
)
有人可以帮助我!
答案 0 :(得分:2)
一个简单的foreach loop或两个可以解决这个问题:
$totals = array();
foreach($array as $current) {
foreach($current as $key => $value) {
if(!array_key_exists($key, $totals))
$totals[$key] = 0;
$totals[$key] += $value;
}
}
print_r($totals);