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 git.master, git.master_jit, rfc.property-hooks
object(stdClass)#1 (3) { ["id"]=> int(0) ["name"]=> string(4) "root" ["children"]=> array(1) { [0]=> object(stdClass)#2 (4) { ["id"]=> int(1) ["parentId"]=> int(0) ["name"]=> string(3) "foo" ["children"]=> array(2) { [0]=> object(stdClass)#3 (3) { ["id"]=> int(2) ["parentId"]=> int(1) ["name"]=> string(3) "bar" } [1]=> object(stdClass)#4 (4) { ["id"]=> int(3) ["parentId"]=> int(1) ["name"]=> string(3) "qux" ["children"]=> array(1) { [0]=> object(stdClass)#5 (4) { ["id"]=> int(4) ["parentId"]=> int(3) ["name"]=> string(3) "zip" ["children"]=> array(1) { [0]=> object(stdClass)#6 (3) { ["id"]=> int(5) ["parentId"]=> int(4) ["name"]=> string(3) "baz" } } } } } } } } }

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:
102.67 ms | 2671 KiB | 4 Q