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("/tmp"); $$t = $$t - microtime(true); echo "$func needed $$t time\n"; }
Output for 5.4.0 - 5.5.24, 5.5.29, 5.5.32 - 5.5.35, 5.6.10, 5.6.13, 5.6.16, 5.6.18 - 7.1.0
dirToArray1 needed $t1 time dirToArray2 needed $t2 time
Output for 5.5.30 - 5.5.31, 5.6.14 - 5.6.15, 5.6.17
dirToArray1 needed $t1 time
Process exited with code 137.
Output for 5.5.27, 5.6.12
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 262144 bytes) in /in/vNuFo on line 6
Process exited with code 255.
Output for 5.5.28, 5.6.8, 5.6.11
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 262144 bytes) in /in/vNuFo on line 12
Process exited with code 255.
Output for 5.6.9
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 72 bytes) in /in/vNuFo on line 6
Process exited with code 255.
Output for 5.5.26
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 72 bytes) in /in/vNuFo on line 8
Process exited with code 255.
Output for 5.5.25
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 10 bytes) in /in/vNuFo on line 6
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/vNuFo on line 20
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/vNuFo on line 20
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/vNuFo on line 20
Process exited with code 255.