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 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.4.16, 8.5.0 - 8.5.1
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 ( )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29
Warning: Trying to access array offset on value of type 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 ( )

preferences:
100.28 ms | 410 KiB | 5 Q