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);
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.13
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) } } } }
Output for 5.4.0 - 5.4.45
Fatal error: Call to undefined function array_column() in /in/kmvuR on line 19
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/kmvuR on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/kmvuR on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/kmvuR on line 3
Process exited with code 255.

preferences:
94.7 ms | 412 KiB | 5 Q