3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function dirToArray1($dir) { $result = array(); $cdir = scandir($dir); foreach ($cdir as $key => $value) { if (!in_array($value,array(".",".."))) { if (is_dir($dir . DIRECTORY_SEPARATOR . $value)){ $result[$value] = dirToArray1($dir . DIRECTORY_SEPARATOR . $value); } else { $result[] = $value; } } } return $result; } function dirToArray2($dir) { $arr = []; $obj = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST); foreach ($obj as $info) { if (in_array($info->getFilename(), ['.', '..'])) continue; $path = $info->isDir() ? [$info->getFilename() => []] : [$info->getFilename()]; for ($depth = $obj->getDepth() - 1; $depth >= 0; $depth--) { $path = [$obj->getSubIterator($depth)->current()->getFilename() => $path]; } $arr = array_merge_recursive($arr, $path); } return $arr; } foreach (['1', '2'] as $k) { $t = "t$k"; $d = "d$k"; $$t = microtime(true); $func = "dirToArray$k"; $$d = $func("."); $$t = $$t - microtime(true); echo "$func needed $$t time\n"; }
Output for 7.0.0 - 7.1.0
Warning: scandir(.): failed to open dir: Permission denied in /in/9MJD9 on line 4 Warning: scandir(): (errno 13): Permission denied in /in/9MJD9 on line 4 Warning: Invalid argument supplied for foreach() in /in/9MJD9 on line 5 dirToArray1 needed $t1 time Fatal error: Uncaught UnexpectedValueException: RecursiveDirectoryIterator::__construct(.): failed to open dir: Permission denied in /in/9MJD9:19 Stack trace: #0 /in/9MJD9(19): RecursiveDirectoryIterator->__construct('.') #1 /in/9MJD9(36): dirToArray2('.') #2 {main} thrown in /in/9MJD9 on line 19
Process exited with code 255.
Output for hhvm-3.10.0 - 3.12.0
Warning: Permission denied in /in/9MJD9 on line 4 Warning: Invalid argument supplied for foreach() in /in/9MJD9 on line 13 dirToArray1 needed $t1 time Fatal error: Uncaught exception 'UnexpectedValueException' with message 'DirectoryIterator::__construct(.): failed to open dir' in /in/9MJD9:19 Stack trace: #0 /in/9MJD9(19): RecursiveDirectoryIterator->__construct() #1 /in/9MJD9(36): dirToArray2() #2 {main}
Process exited with code 255.
Output for 5.4.0 - 5.6.28
Warning: scandir(.): failed to open dir: Permission denied in /in/9MJD9 on line 4 Warning: scandir(): (errno 13): Permission denied in /in/9MJD9 on line 4 Warning: Invalid argument supplied for foreach() in /in/9MJD9 on line 5 dirToArray1 needed $t1 time Fatal error: Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(.): failed to open dir: Permission denied' in /in/9MJD9:19 Stack trace: #0 /in/9MJD9(19): RecursiveDirectoryIterator->__construct('.') #1 /in/9MJD9(36): dirToArray2('.') #2 {main} thrown in /in/9MJD9 on line 19
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/9MJD9 on line 18
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/9MJD9 on line 18
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/9MJD9 on line 18
Process exited with code 255.