3v4l.org

run code in 300+ PHP versions simultaneously
<?php $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], ]; $teampeople = array_column($people, "team"); Foreach($team as &$t){ $t['members'] = array_values(array_intersect_key($people, array_intersect($teampeople, [$t['id']]))); } Var_dump($team);

preferences:
64.45 ms | 408 KiB | 5 Q