Merging and summing multi-dimensional arrays I've found myself today really struggling with this subject and I'm pretty sure that there must be a better way to do this.

First of all my arrays have the same layout, I need to merge them and sum every value by domain. 

Let me give you an example:

$array[0]['X1']['Y1'] = 1;
$array[0]['X1']['Y2'] = 2;
$array[0]['INFO']['DOMAIN'] = 'example1.com';

$array[1]['X1']['Y1'] = 1;
$array[1]['X1']['Y2'] = 2;
$array[1]['INFO']['DOMAIN'] = 'example1.com';

$array[2]['X1']['Y1'] = 2;
$array[2]['X1']['Y2'] = 5;
$array[2]['INFO']['DOMAIN'] = 'example2.com';

$array[3]['X1']['Y1'] = 2;
$array[3]['X1']['Y2'] = 5;
$array[3]['INFO']['DOMAIN'] = 'example2.com';


I need to produce the following new array:

$mergedArray[0]['X1']['Y1'] = 2;
$mergedArray[0]['X1']['Y2'] = 4;
$mergedArray[0]['INFO']['DOMAIN'] = 'example1.com'; 

$mergedArray[1]['X1']['Y1'] = 4;
$mergedArray[1]['X1']['Y2'] = 10;
$mergedArray[1]['INFO']['DOMAIN'] = 'example2.com'; 


I'm using the following code to achieve this (forgive me for the bad namings).

foreach($array as $subKey =&gt; $subValue) {
    foreach($subValue as $key =&gt; $value) {
        foreach($value as $key2 =&gt; $value2) {
            if($key2=='DOMAIN' &amp;&amp; !isset($merged[$value2])) {
                $merged[$value2] = $subValue;
            } else if ($key2=='DOMAIN' &amp;&amp; isset($merged[$value2])) {
                foreach($subValue as $key3=&gt;$value3) {
                    foreach($value3 as $key4=&gt;$value4) {
                        if($key4!='DOMAIN') 
                            $merged[$value2][$key3][$key4] += $value4;
                    }
                }
            }
        }
    }
}


How could I improve this piece of code?
Presuming that the format will be consistent, you could remove one foreach loop and extract the domain to use as the key of the merged array, and also only either set the sub array (with keys X1, Y1 and Y2) or add to the sub array items. 

foreach($array as $subArray) {
    if (isset($subArray['INFO']) &amp;&amp; isset($subArray['INFO']['DOMAIN'])) {
        $domainKey = $subArray['INFO']['DOMAIN'];
        if (!isset($merged[$domainKey])) {
            $merged[$domainKey] = $subArray;
        }
        else { // could add conditionals to ensure X1 is set on $subArray
            foreach($subArray['X1'] as $key =&gt; $value) {
                $merged[$domainKey]['X1'][$key] += $value;
            }
        }
    }
}


See it in action in this playground example.

A functional approach could also be employed by using array_reduce():

$merged = array_reduce($array, function($cumulative, $subArray) {
    if (isset($subArray['INFO']) &amp;&amp; isset($subArray['INFO']['DOMAIN'])) {
        $domainKey = $subArray['INFO']['DOMAIN'];
        if (!isset($cumulative[$domainKey])) {
            $cumulative[$domainKey] = $subArray;
        }
        else { // could add conditionals to ensure X1 is set on $subArray
            foreach($subArray['X1'] as $key =&gt; $value) {
                $cumulative[$domainKey]['X1'][$key] += $value;
            }
        }
    }
    return $cumulative;
}, []);


See it in action in this playground example