3v4l.org

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

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.22 ms | 405 KiB | 8 Q