proph3t proph3t - 3 months ago 6
PHP Question

PHP Associative Array Merge On Key

I am working to combine the following arrays:

Array #1

[0] => Store1
[1] => Array (
[ytd] => Array (
[newups] => 1837
[usedups] => 1777
[totalups] => 3614
[totalsales] => 1446
)
[prevyear] => Array (
[newups] => 262
[usedups] => 281
[totalups] => 543
[totalsales] => 240
)
[prevmonth] => Array (
[goals] => Array (
[jangoal] => 236
[febgoal] => 224
[margoal] => 243
[aprgoal] => 218
[maygoal] => 221
[jungoal] => 239
[julgoal] => 241
[auggoal] => 0
)
[ups] => Array (
[newups] => Array (
[2016-01] => 193
[2016-02] => 0
[2016-03] => 0
[2016-04] => 0
[2016-05] => 0
[2016-06] => 0
[2016-07] => 0
[2016-08] => 0
)
)
)
[year] => 2016
[month] => September
[goalsales] => 197
[daily] => Array (
[ups] => Array (
[2016-09-01] => 18
[2016-09-02] => 0
[2016-09-03] => 0
[2016-09-04] => 0
[2016-09-05] => 0
[2016-09-06] => 0
[2016-09-07] => 0
[2016-09-08] => 0
[2016-09-09] => 0
[2016-09-10] => 0
[2016-09-11] => 0
[2016-09-12] => 0
[2016-09-13] => 0
[2016-09-14] => 0
[2016-09-15] => 0
)
[sold] => Array (
[2016-09-01] => 4
[2016-09-02] => 0
[2016-09-03] => 0
[2016-09-04] => 0
[2016-09-05] => 0
[2016-09-06] => 0
[2016-09-07] => 0
[2016-09-08] => 0
[2016-09-09] => 0
[2016-09-10] => 0
[2016-09-11] => 0
[2016-09-12] => 0
[2016-09-13] => 0
[2016-09-14] => 0
[2016-09-15] => 0
)
)
)


Array #2

[2] => Store2
[3] => Array (
[ytd] => Array (
[newups] => 626
[usedups] => 568
[totalups] => 1194
[totalsales] => 419
)
[prevyear] => Array (
[newups] => 96
[usedups] => 102
[totalups] => 198
[totalsales] => 81
)
[prevmonth] => Array (
[goals] => Array (
[jangoal] => 68
[febgoal] => 70
[margoal] => 75
[aprgoal] => 71
[maygoal] => 69
[jungoal] => 75
[julgoal] => 91
[auggoal] => 0
)
[ups] => Array (
[newups] => Array (
[2016-01] => 52
[2016-02] => 0
[2016-03] => 0
[2016-04] => 0
[2016-05] => 0
[2016-06] => 0
[2016-07] => 0
[2016-08] => 0
)
)
)
[year] => 2016
[month] => September
[goalsales] => 66
[daily] => Array (
[ups] => Array (
[2016-09-01] => 8
[2016-09-02] => 0
[2016-09-03] => 0
[2016-09-04] => 0
[2016-09-05] => 0
[2016-09-06] => 0
[2016-09-07] => 0
[2016-09-08] => 0
[2016-09-09] => 0
[2016-09-10] => 0
[2016-09-11] => 0
[2016-09-12] => 0
[2016-09-13] => 0
[2016-09-14] => 0
[2016-09-15] => 0
)
[sold] => Array (
[2016-09-01] => 7
[2016-09-02] => 0
[2016-09-03] => 0
[2016-09-04] => 0
[2016-09-05] => 0
[2016-09-06] => 0
[2016-09-07] => 0
[2016-09-08] => 0
[2016-09-09] => 0
[2016-09-10] => 0
[2016-09-11] => 0
[2016-09-12] => 0
[2016-09-13] => 0
[2016-09-14] => 0
[2016-09-15] => 0
)
)
)


The goal is to end up with a combined array (like the example below) in which both arrays have been combined recursively to maintain the array key structure, but return the sum of values:

Combined Array

[1] => Array (
[ytd] => Array (
[newups] => 2463
[usedups] => 2354
[totalups] => 4808
[totalsales] => 1865
)
[prevyear] => Array (
[newups] => 358
[usedups] => 383
[totalups] => 543
[totalsales] => 240
)
[prevmonth] => Array (
[goals] => Array (
[jangoal] => 236
[febgoal] => 224
[margoal] => 243
[aprgoal] => 218
[maygoal] => 221
[jungoal] => 239
[julgoal] => 241
[auggoal] => 0
)
[ups] => Array (
[newups] => Array (
[2016-01] => 193
[2016-02] => 0
[2016-03] => 0
[2016-04] => 0
[2016-05] => 0
[2016-06] => 0
[2016-07] => 0
[2016-08] => 0
)
)
)
[year] => 2016
[month] => September
[goalsales] => 197
[daily] => Array (
[ups] => Array (
[2016-09-01] => 18
[2016-09-02] => 0
[2016-09-03] => 0
[2016-09-04] => 0
[2016-09-05] => 0
[2016-09-06] => 0
[2016-09-07] => 0
[2016-09-08] => 0
[2016-09-09] => 0
[2016-09-10] => 0
[2016-09-11] => 0
[2016-09-12] => 0
[2016-09-13] => 0
[2016-09-14] => 0
[2016-09-15] => 0
)
[sold] => Array (
[2016-09-01] => 4
[2016-09-02] => 0
[2016-09-03] => 0
[2016-09-04] => 0
[2016-09-05] => 0
[2016-09-06] => 0
[2016-09-07] => 0
[2016-09-08] => 0
[2016-09-09] => 0
[2016-09-10] => 0
[2016-09-11] => 0
[2016-09-12] => 0
[2016-09-13] => 0
[2016-09-14] => 0
[2016-09-15] => 0
)
)
)


I have tried to following code, but the output does not return the expected values:

foreach ($array as $value){
$id = $value[];
if ( !isset($output[$id]) ) {
$output[$id] = array();
}
$output[$id] = array_merge($output[$id], $value);
}


Any help would be appreciated.

Answer

if i understand your question correctly, then this is how you can combine your arrays:

function merge($arr1, $arr2)
{
    $arr = [];

    foreach ($arr1 as $key => $value) {
        if (is_array($arr1[$key])) {
            $arr[$key] = merge($arr1[$key], $arr2[$key]);
        } else {
            $arr[$key] = $arr1[$key] + $arr2[$key];
        }
    }

    return $arr;
}