如何通过合并值求和来合并两个阵列 [英] How to merge two arrays by summing the merged values
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/1496682/php-how-to-sum-values-of-the-array-of-the-same-key\">PHP:如何总结相同的密钥数组的值
块引用>我要寻找一个
array_merge()
函数不替代价值,但增加了他们。例如,这是code我想:
回声&LT; pre&gt;中; $ A1 =阵列(
一个=&GT; 2
,B=&GT; 0
,C=&GT;五
); $ A2 =阵列(
一个=&GT; 3
,B=&GT; 9
,C=&GT; 7
,D=&GT; 10
); $ A3 = array_merge($ A1,A2 $);
的print_r($ A3);不幸的是,这本输出:
阵列
(
[α] =&GT; 3
并[b] =&GT; 9
[C] =&GT; 7
并[d] =&GT; 10
)我又试图,而不是
array_merge
,只是简单地将两个数组$ A3 = $ A1 + A2 $;
但这种输出
阵列
(
[α] =&GT; 2
并[b] =&GT; 0
[C] =&GT;五
并[d] =&GT; 10
)我真正想要的是能够根据需要通过尽可能多的阵列,然后得到他们的总和。所以在我的例子中,我所要的输出是:
阵列
(
[α] =&GT;五
并[b] =&GT; 9
[C] =&GT; 12
并[d] =&GT; 10
)我当然可以拖带,建设有许多
的foreach
等一些功能,但我还是期待一个更聪明,更清洁的解决方案。感谢您的指点!解决方案$款项=阵列();
的foreach(array_keys($ A1 + A2 $)为$键){
$款项[$关键] =(使用isset($ A1 [$关键])$ A1 [$关键]:0?)+(使用isset($ A2 [$关键])$ A2 [$关键]:0);
}您能的缩短这对使用误差SUP pression操作者以下,但它应被视为难看:
$款项=阵列();
的foreach(array_keys($ A1 + A2 $)为$键){
$款项[$关键] = @($ A1 [$键] + $ A2 [$关键]);
}另外,一些映射:
$键= array_fill_keys(array_keys($ A1 + A2 $),0);
$款项= array_map(功能($ A1,A2 $){$回报A1 + A2 $;},array_merge($键,$ A1),array_merge($键,$ A2));或排序两种溶液的组合:
$款项= array_fill_keys(array_keys($ A1 + A2 $),0);
array_walk($款项,功能(安培; $价值$键,$ ARRS){$值= @($ ARRS [0] [$键] + $ ARRS [1] [$关键]);},数组($ A1,$ A2));我觉得这些都是足够简洁,以适应其中一人当场需要的时候,但把它放在接受阵列的数量不受限制,总结他们的功能方面:
函数array_sum_identical_keys(){
$数组= func_get_args();
$键= array_keys(array_reduce($阵列功能($键,$ ARR){$返回键+ $改编;},阵列()));
$款项=阵列(); 的foreach($键为$键){
$款项[$关键] = array_reduce($阵列功能($总和,$ ARR)使用($键){返回$总和+ @ $改编[$关键];});
}
返回$款项;
}Possible Duplicate:
PHP: How to sum values of the array of the same keyI am looking for an
array_merge()
function that does NOT replace values, but ADDS them.Example, this is the code I am trying:
echo "<pre>"; $a1 = array( "a" => 2 ,"b" => 0 ,"c" => 5 ); $a2 = array( "a" => 3 ,"b" => 9 ,"c" => 7 ,"d" => 10 ); $a3 = array_merge($a1, $a2); print_r($a3);
Sadly, this outputs this:
Array ( [a] => 3 [b] => 9 [c] => 7 [d] => 10 )
I then tried, instead of
array_merge
, just simply adding the two arrays$a3 = $a1 + $a2;
But this outputs
Array ( [a] => 2 [b] => 0 [c] => 5 [d] => 10 )
What I truly want is to be able to pass as many arrays as needed, and then get their sum. So in my example, I want the output to be:
Array ( [a] => 5 [b] => 9 [c] => 12 [d] => 10 )
Of course I can schlepp and build some function with many
foreach
etc, but am looking or a smarter, cleaner solution. Thanks for any pointers!解决方案$sums = array(); foreach (array_keys($a1 + $a2) as $key) { $sums[$key] = (isset($a1[$key]) ? $a1[$key] : 0) + (isset($a2[$key]) ? $a2[$key] : 0); }
You could shorten this to the following using the error suppression operator, but it should be considered ugly:
$sums = array(); foreach (array_keys($a1 + $a2) as $key) { $sums[$key] = @($a1[$key] + $a2[$key]); }
Alternatively, some mapping:
$keys = array_fill_keys(array_keys($a1 + $a2), 0); $sums = array_map(function ($a1, $a2) { return $a1 + $a2; }, array_merge($keys, $a1), array_merge($keys, $a2));
Or sort of a combination of both solutions:
$sums = array_fill_keys(array_keys($a1 + $a2), 0); array_walk($sums, function (&$value, $key, $arrs) { $value = @($arrs[0][$key] + $arrs[1][$key]); }, array($a1, $a2));
I think these are concise enough to adapt one of them on the spot whenever needed, but to put it in terms of a function that accepts an unlimited number of arrays and sums them:
function array_sum_identical_keys() { $arrays = func_get_args(); $keys = array_keys(array_reduce($arrays, function ($keys, $arr) { return $keys + $arr; }, array())); $sums = array(); foreach ($keys as $key) { $sums[$key] = array_reduce($arrays, function ($sum, $arr) use ($key) { return $sum + @$arr[$key]; }); } return $sums; }
这篇关于如何通过合并值求和来合并两个阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!