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

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成