3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); $items = [ (object)['id' => 0, 'name' => "root"] (object)['id' => 1, 'parentId' => 0, 'name' => "foo"], (object)['id' => 2, 'parentId' => 1, 'name' => "bar"], (object)['id' => 3, 'parentId' => 1, 'name' => "qux"], (object)['id' => 4, 'parentId' => 3, 'name' => "zip"], (object)['id' => 5, 'parentId' => 4, 'name' => "baz"] ]; $itemsById = []; foreach ($items as $item) { $itemsById[$item->id] = $item; } // --- foreach ($itemsById as $id => $item) { if (isset($item->parentId)) { $itemsById[$item->parentId]->children[] = $item; } } $root = $itemsById[0]; var_dump($root);
Output for 7.0.0
Parse error: syntax error, unexpected '(object)' (object) (T_OBJECT_CAST), expecting ']' in /in/TiShB on line 6
Process exited with code 255.
Output for 5.5.24 - 5.5.30, 5.6.8 - 5.6.16
Warning: Unsupported declare 'strict_types' in /in/TiShB on line 2 Parse error: syntax error, unexpected '(object)' (object) (T_OBJECT_CAST), expecting ']' in /in/TiShB on line 6
Process exited with code 255.

preferences:
165.82 ms | 1395 KiB | 24 Q