3v4l.org

run code in 300+ PHP versions simultaneously
<?php $items = array( (object) array('id' => 1, 'parent_id' => 0), (object) array('id' => 2, 'parent_id' => 0), (object) array('id' => 3, 'parent_id' => 0), (object) array('id' => 4, 'parent_id' => 1), (object) array('id' => 5, 'parent_id' => 1), (object) array('id' => 6, 'parent_id' => 2), ); function buildTree($items) { $childs = array(); foreach($items as $item) $childs[$item->parent_id][] = $item; foreach($items as $item) if (isset($childs[$item->id])) $item->childs = $childs[$item->id]; return $childs[0]; } $tree = buildTree($items); print_r($tree);
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [0] => stdClass Object ( [id] => 1 [parent_id] => 0 [childs] => Array ( [0] => stdClass Object ( [id] => 4 [parent_id] => 1 ) [1] => stdClass Object ( [id] => 5 [parent_id] => 1 ) ) ) [1] => stdClass Object ( [id] => 2 [parent_id] => 0 [childs] => Array ( [0] => stdClass Object ( [id] => 6 [parent_id] => 2 ) ) ) [2] => stdClass Object ( [id] => 3 [parent_id] => 0 ) )

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:
41.16 ms | 403 KiB | 8 Q