duanji4449 2016-09-02 20:03
浏览 18
已采纳

PHP关联数组合并键

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.

  • 写回答

2条回答 默认 最新

  • 普通网友 2016-09-02 20:23
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错