3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ 'dir1' => [ 'dir2' => [ 'dir3' => [ 'dir4' => [ 'file1.php', 'abc.php', ] ], 'file2.php', 'abc.php', ] ], 'bar' => [ 'foo12' => ['blah', 'blar', 'blag', 'blas'], 'foo9' => ['boom6', 'boom55', 'boom123'], 'foo888' => [], ], 'abc' => [ 'abc' => [ 'abc' => [ 'file5.php', ] ] ] ]; function sortFileSystem(array &$array) { $dirs = []; $files = []; foreach ($array as $k => &$v) { if (is_array($v)) { (__FUNCTION__)($v); $dirs[$k] = $v; // preserve key } else { $files[] = $v; // key is unneeded } } ksort($dirs, SORT_NATURAL); sort($files, SORT_NATURAL); $array = array_merge($dirs, $files); // put dirs before files } sortFileSystem($array); var_export($array);

preferences:
26.75 ms | 407 KiB | 5 Q