dragon8837 2018-08-15 11:17
浏览 86
已采纳

PHP:如何按键合并两个嵌套数组

I have two arrays like:

$team = [
    ['id' => 1, 'name' => 'Team A'],
    ['id' => 2, 'name' => 'Team B'],
    ['id' => 3, 'name' => 'Team C'], 
]; 

$people = [
    ['id' => 1, 'name' => 'Mark Hamill', 'team' => 1],
    ['id' => 2, 'name' => 'Nicolas Cage', 'team' => 2],
    ['id' => 3, 'name' => 'Tom Cruise', 'team' => 3],
    ['id' => 4, 'name' => 'Tom Hanks', 'team' => 1],
    ['id' => 5, 'name' => 'Brad Pitt', 'team' => 2],
    ['id' => 6, 'name' => 'Paul Smith', 'team' => 3],
    ['id' => 7, 'name' => 'Matt Daemon', 'team' => 1],
    ['id' => 8, 'name' => 'Robert Redford', 'team' => 2],
]  

I would like to merge the $people array into the $team array as a child node based on the team id. So the result would be:

$team = [
    [
        'id' => 1, 
        'name' =>'Team A',
        'members' => [
            ['id' => 1, 'name' => 'Mark Hamill', 'team' => 1],
            ['id' => 4, 'name' => 'Tom Hanks', 'team' => 1],
            ['id' => 7, 'name' => 'Matt Daemon', 'team' => 1],
        ]
    ],
    [
        'id' => 2, 
        'name' =>'Team B',
        'members' => [
            ['id' => 2, 'name' => 'Nicolas Cage', 'team' => 2],
            ['id' => 5, 'name' => 'Brad Pitt', 'team' => 2],
            ['id' => 8, 'name' => 'Robert Redford', 'team' => 2],
        ]
    ],
    [
        'id' => 3, 
        'name' =>'Team C',
        'members' => [
            ['id' => 3, 'name' => 'Tom Cruise', 'team' => 3],
            ['id' => 6, 'name' => 'Paul Smith', 'team' => 3],
        ]
    ], 
]; 

I know I can loop through $team and add the relevant $people one at a time based on their 'team' id, but I was wondering if there was a more efficient way of doing this. In my project, either of the arrays could grow to contain up to around 50 items each and processing these one at a time is really slowing the page down.

Thanks

  • 写回答

2条回答 默认 最新

  • duanbo5230 2018-08-15 11:30
    关注

    This will loop your $teams array and intersect with an array_column to get the arrays you want.

    $teampeople = array_column($people, "team");
    //Creates a lookup array of the teams from people array
    
    foreach($team as &$t){
        // Here I match from the lookup array and get the "main" arrays.  
        // Array_values remove the indexed from the resulting array to make it 0,1,2 etc.
        $t['members'] = array_values(array_intersect_key($people, array_intersect($teampeople, [$t['id']])));
    }
    unset($t); // just to make sure you don't accidentally change the array
    var_dump($team);
    

    Outputs:

    array(3) {
      [0]=>
      array(3) {
        ["id"]=>
        int(1)
        ["name"]=>
        string(6) "Team A"
        ["members"]=>
        array(3) {
          [0]=>
          array(3) {
            ["id"]=>
            int(1)
            ["name"]=>
            string(11) "Mark Hamill"
            ["team"]=>
            int(1)
          }
          [1]=>
          array(3) {
            ["id"]=>
            int(4)
            ["name"]=>
            string(9) "Tom Hanks"
            ["team"]=>
            int(1)
          }
          [2]=>
          array(3) {
            ["id"]=>
            int(7)
            ["name"]=>
            string(11) "Matt Daemon"
            ["team"]=>
            int(1)
          }
        }
      }
      [1]=>
      array(3) {
        ["id"]=>
        int(2)
        ["name"]=>
        string(6) "Team B"
        ["members"]=>
        array(3) {
          [0]=>
          array(3) {
            ["id"]=>
            int(2)
            ["name"]=>
            string(12) "Nicolas Cage"
            ["team"]=>
            int(2)
          }
          [1]=>
          array(3) {
            ["id"]=>
            int(5)
            ["name"]=>
            string(9) "Brad Pitt"
            ["team"]=>
            int(2)
          }
          [2]=>
          array(3) {
            ["id"]=>
            int(8)
            ["name"]=>
            string(14) "Robert Redford"
            ["team"]=>
            int(2)
          }
        }
      }
      [2]=>
      &array(3) {
        ["id"]=>
        int(3)
        ["name"]=>
        string(6) "Team C"
        ["members"]=>
        array(2) {
          [0]=>
          array(3) {
            ["id"]=>
            int(3)
            ["name"]=>
            string(10) "Tom Cruise"
            ["team"]=>
            int(3)
          }
          [1]=>
          array(3) {
            ["id"]=>
            int(6)
            ["name"]=>
            string(10) "Paul Smith"
            ["team"]=>
            int(3)
          }
        }
      }
    }
    

    https://3v4l.org/kmvuR

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效