0

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.

proph3t
  • 865
  • 2
  • 7
  • 25
  • Have you tried array_merge_recursive? http://php.net/manual/en/function.array-merge-recursive.php – Lynne Sep 02 '16 at 20:12
  • Possible duplicate of [Array merge on key of two associative arrays in php?](http://stackoverflow.com/questions/9112920/array-merge-on-key-of-two-associative-arrays-in-php) – Kamlesh Gupta Sep 02 '16 at 20:15
  • Please always provide your array data as `var_export()` text so that contributors can instantly use your data while testing their solutions. – mickmackusa Aug 21 '22 at 04:04

2 Answers2

1

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;
}
Adam Russell
  • 171
  • 1
  • 4
-1

You might want to use the function array_merge_recursive to merge the 2 arrays.

$newarray = array_merge_recursive($array1, $array2);

Then you need to go through your new array and search recursively the key 0 and 1, and add their values.

delpi
  • 99
  • 5
  • This won't add the values when the same key exists in both arrays, it will just use one of the values. – Barmar Sep 02 '16 at 21:02
  • "If the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended." So there is no overwrite. That way, you can sum the 2 on the next process. – delpi Sep 06 '16 at 18:46
  • It looks to me like "the next process" is the part he doesn't know how to do. – Barmar Sep 06 '16 at 18:48