3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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);
based on OBh3k
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.4.10 - 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.
Output for 5.3.20 - 5.3.29
Warning: Unsupported declare 'strict_types' in /in/TiShB on line 2 Parse error: syntax error, unexpected '[' in /in/TiShB on line 4
Process exited with code 255.