3v4l.org

run code in 300+ PHP versions simultaneously
<?php function buildTree(array $rows, int $parentId = 0): array { $new = []; foreach ($rows as $row) { if ($row['parent_id'] == $parentId) { $new[$row['id']] = buildTree($rows, $row['id']) ?: ''; } } return $new; } $resultSet = [ ['id' => 1, 'parent_id' => 0], ['id' => 2, 'parent_id' => 1], ['id' => 3, 'parent_id' => 2], ['id' => 4, 'parent_id' => 2], ['id' => 5, 'parent_id' => 3], ['id' => 6, 'parent_id' => 3], ['id' => 7, 'parent_id' => 4], ['id' => 8, 'parent_id' => 4], ['id' => 9, 'parent_id' => 4], ]; var_export( buildTree($resultSet) );
Output for git.master, git.master_jit, rfc.property-hooks
array ( 1 => array ( 2 => array ( 3 => array ( 5 => '', 6 => '', ), 4 => array ( 7 => '', 8 => '', 9 => '', ), ), ), )

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:
29.5 ms | 406 KiB | 5 Q