$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;
}