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 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
array ( 1 => array ( 2 => array ( 3 => array ( 5 => '', 6 => '', ), 4 => array ( 7 => '', 8 => '', 9 => '', ), ), ), )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
147.04 ms | 407 KiB | 5 Q