3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [ 'history' => [ 'id' => 23452435, 'legend' => [ ['player' => 'me', 'turn' => 1, 'card' => ['name' => 'foo']], ['player' => 'me', 'turn' => 1, 'card' => ['name' => 'bar']], ['player' => 'opponent', 'turn' => 1, 'card' => ['name' => 'derp']], ['player' => 'opponent', 'turn' => 2, 'card' => ['name' => 'hoo']], ], ] ]; $historyByTurns = array(); foreach ($arr['history'] as $historyItem) { foreach ($historyItem['legend'] as $legendItem) { $turn = $legendItem['turn']; $player = $legendItem['player']; if (!array_key_exists($turn, $historyByTurns)) { $historyByTurns[$turn] = array(); } if (!array_key_exists($player, $historyByTurns[$turn])) { $historyByTurns[$turn][$player] = array(); } foreach ($legendItem as $card) { $historyByTurns[$turn][$player][] = $card['name']; } } } var_export($historyByTurns);
Output for git.master_jit, git.master, rfc.property-hooks
Warning: Trying to access array offset on int in /in/kH0BS on line 17 Warning: foreach() argument must be of type array|object, null given in /in/kH0BS on line 17 Warning: Undefined array key "legend" in /in/kH0BS on line 17 Warning: foreach() argument must be of type array|object, null given in /in/kH0BS on line 17 array ( )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
33.71 ms | 406 KiB | 5 Q