簡體   English   中英

PHP:如何通過鍵合並兩個嵌套的數組

[英]PHP: How to merge two nested arrays by key

我有兩個像這樣的數組:

$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],
]  

我想根據團隊ID將$ people數組合並為$ team數組作為子節點。 因此結果將是:

$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],
        ]
    ], 
]; 

我知道我可以遍歷$ team並根據他們的“ team” ID一次添加一個相關的$ people,但是我想知道是否有更有效的方法來做到這一點。 在我的項目中,這兩個數組中的每個數組都可以增長到最多包含大約50個項目,並且一次處理這些項目確實會使頁面速度變慢。

謝謝

這將循環$ teams數組,並與array_column相交以獲得所需的數組。

$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);

輸出:

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

嘗試這個:

foreach ($team as $t) {
    $t['members'] = array_filter($people, function ($var) use ($t) {
    return ($var['team'] == $t['id']);
})
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM