0

I have a multidimensional array that looks like this:

Array
(
    [0] => Array
    (
        [GB] => 20827
        [US] => 73
        [ES] => 23
        [AU] => 15
        [DE] => 12
        [MY] => 4
        [QA] => 1
        [VN] => 1
        [SK] => 1
        [FJ] => 1
        [ME] => 1
        [TR] => 1
        [LV] => 1
    )

    [1] => Array
    (
        [GB] => 15070
        [US] => 3920
        [IE] => 1711
        [PH] => 1071
        [MT] => 578
        [AU] => 423
        [HR] => 241
        [ZA] => 210
        [FR] => 139
    )
)

I want to create a new array with all of the associative elements in each array of the multidimensional array and sum the values of duplicated keys.

Desired result from sample input:

array (
  'GB' => 35897,
  'US' => 3993,
  'ES' => 23,
  'AU' => 438,
  'DE' => 12,
  'MY' => 4,
  'QA' => 1,
  'VN' => 1,
  'SK' => 1,
  'FJ' => 1,
  'ME' => 1,
  'TR' => 1,
  'LV' => 1,
  'IE' => 1711,
  'PH' => 1071,
  'MT' => 578,
  'HR' => 241,
  'ZA' => 210,
  'FR' => 139,
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
jackthedev
  • 417
  • 8
  • 21

3 Answers3

1

Look through each of the sub-arrays, and then loop through the key/value pairs. Add them up in a $final array:

$final = [];
foreach($masterArray as $subArray) {
    foreach($subArray AS $key => $value) {
        $final[$key] = isset($final[$key])
            ? $final[$key] + $value
            : $value;
    }
}

Working example: https://3v4l.org/6XBbD

jszobody
  • 28,495
  • 6
  • 61
  • 72
0

perhaps something like

$array = count($array1) > count($array2) ? $array1 : $array2;
$otherArray = count($array1) > count($array2) ? $array2 : $array1;

array_walk($array, function(&$value, $key, $otherArray) {
    $value = $value + (isset($otherArray[$key]) ? $otherArray[$key] : 0);
}, $otherArray);
Tyler Sebastian
  • 9,067
  • 6
  • 39
  • 62
0

You can fast track this task by "shifting" the first row of data off of the input array and pushing the associative elements directly into the output array. This removes the need to execute any conditional operations on the first row which will improve efficiency.

Code: (Demo)

$result = array_shift($array);
foreach ($array as $row) {
    foreach ($row as $key => $value) {
        $result[$key] = ($result[$key] ?? 0) + $value;
    }
}
var_export($result);

Alterative with array_reduce(): (Demo)

$result = array_shift($array);
var_export(
    array_reduce(
        $array,
        function($result, $row) {
            foreach ($row as $key => $value) {
                $result[$key] = ($result[$key] ?? 0) + $value;
            }
            return $result;
        },
        $result
    )
);

Alternative with array_merge_recursive(): (Demo)

$result = array_shift($array);
array_walk_recursive(
    $array,
    function($value, $key) use(&$result) {
        $result[$key] = ($result[$key] ?? 0) + $value;
    }
);
var_export($result);

As a side effect of using array_shift(), if the input array is empty, then the output of all snippets above will be null instead of an empty array. If this is a deal-breaker for your project, then replace array_shift() with an empty array -- Demo.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136