PHP关联数组合并键

时间:2016-09-02 20:03:45

标签: php arrays

我正在努力合并以下数组:

数组#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
        )
    )
)

数组#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
        )
    )
)

目标是最终得到一个组合数组(如下例所示),其中两个数组已递归组合以维护数组键结构,但返回值的总和:

组合数组

[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
        )
    )
)

我试过跟随代码,但输出没有返回预期的值:

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

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

如果我理解你的问题,那么这就是你如何组合你的数组:

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

答案 1 :(得分:-1)

您可能希望使用函数array_merge_recursive来合并2个数组。

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

然后你需要遍历你的新数组并递归搜索键0和1,并添加它们的值。