3v4l.org

run code in 300+ PHP versions simultaneously
<?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"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 1, Position 2 = 34
Branch analysis from position: 1
2 jumps found. (Code = 78) Position 1 = 2, Position 2 = 34
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 1
Branch analysis from position: 1
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
filename:       /in/9MJD9
function name:  (null)
number of ops:  36
compiled vars:  !0 = $k, !1 = $t, !2 = $d, !3 = $func
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E > > FE_RESET_R                                       $4      <array>, ->34
          1    > > FE_FETCH_R                                               $4, !0, ->34
   32     2    >   NOP                                                      
          3        FAST_CONCAT                                      ~5      't', !0
          4        ASSIGN                                                   !1, ~5
   33     5        NOP                                                      
          6        FAST_CONCAT                                      ~7      'd', !0
          7        ASSIGN                                                   !2, ~7
   34     8        INIT_FCALL                                               'microtime'
          9        SEND_VAL                                                 <true>
         10        DO_ICALL                                         $10     
         11        FETCH_W                      local               $9      !1
         12        ASSIGN                                                   $9, $10
   35    13        NOP                                                      
         14        FAST_CONCAT                                      ~12     'dirToArray', !0
         15        ASSIGN                                                   !3, ~12
   36    16        INIT_DYNAMIC_CALL                                        !3
         17        SEND_VAL_EX                                              '.'
         18        DO_FCALL                                      0  $15     
         19        FETCH_W                      local               $14     !2
         20        ASSIGN                                                   $14, $15
   37    21        FETCH_R                      local               ~18     !1
         22        INIT_FCALL                                               'microtime'
         23        SEND_VAL                                                 <true>
         24        DO_ICALL                                         $19     
         25        SUB                                              ~20     ~18, $19
         26        FETCH_W                      local               $17     !1
         27        ASSIGN                                                   $17, ~20
   38    28        ROPE_INIT                                     4  ~23     !3
         29        ROPE_ADD                                      1  ~23     ~23, '+needed+%24'
         30        ROPE_ADD                                      2  ~23     ~23, !1
         31        ROPE_END                                      3  ~22     ~23, '+time%0A'
         32        ECHO                                                     ~22
   31    33      > JMP                                                      ->1
         34    >   FE_FREE                                                  $4
   39    35      > RETURN                                                   1

Function dirtoarray1:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 29
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 29
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 28
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 26
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 28
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
filename:       /in/9MJD9
function name:  dirToArray1
number of ops:  32
compiled vars:  !0 = $dir, !1 = $result, !2 = $cdir, !3 = $value, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
    3     1        ASSIGN                                                   !1, <array>
    4     2        INIT_FCALL                                               'scandir'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $6      
          5        ASSIGN                                                   !2, $6
    5     6      > FE_RESET_R                                       $8      !2, ->29
          7    > > FE_FETCH_R                                       ~9      $8, !3, ->29
          8    >   ASSIGN                                                   !4, ~9
    6     9        IN_ARRAY                                         ~11     !3, <array>
         10        BOOL_NOT                                         ~12     ~11
         11      > JMPZ                                                     ~12, ->28
    7    12    >   INIT_FCALL                                               'is_dir'
         13        CONCAT                                           ~13     !0, '%2F'
         14        CONCAT                                           ~14     ~13, !3
         15        SEND_VAL                                                 ~14
         16        DO_ICALL                                         $15     
         17      > JMPZ                                                     $15, ->26
    8    18    >   INIT_FCALL_BY_NAME                                       'dirToArray1'
         19        CONCAT                                           ~17     !0, '%2F'
         20        CONCAT                                           ~18     ~17, !3
         21        SEND_VAL_EX                                              ~18
         22        DO_FCALL                                      0  $19     
         23        ASSIGN_DIM                                               !1, !3
         24        OP_DATA                                                  $19
         25      > JMP                                                      ->28
   10    26    >   ASSIGN_DIM                                               !1
         27        OP_DATA                                                  !3
    5    28    > > JMP                                                      ->7
         29    >   FE_FREE                                                  $8
   14    30      > RETURN                                                   !1
   15    31*     > RETURN                                                   null

End of function dirtoarray1

Function dirtoarray2:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 53
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 53
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 17
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 25
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 35
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 35
Branch analysis from position: 47
Branch analysis from position: 35
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
Branch analysis from position: 53
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 53
filename:       /in/9MJD9
function name:  dirToArray2
number of ops:  56
compiled vars:  !0 = $dir, !1 = $arr, !2 = $obj, !3 = $info, !4 = $path, !5 = $depth
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   18     1        ASSIGN                                                   !1, <array>
   19     2        NEW                                              $7      'RecursiveIteratorIterator'
          3        NEW                                              $8      'RecursiveDirectoryIterator'
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0          
          6        SEND_VAR_NO_REF_EX                                       $8
          7        SEND_VAL_EX                                              2
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !2, $7
   20    10      > FE_RESET_R                                       $12     !2, ->53
         11    > > FE_FETCH_R                                               $12, !3, ->53
   21    12    >   INIT_METHOD_CALL                                         !3, 'getFilename'
         13        DO_FCALL                                      0  $13     
         14        IN_ARRAY                                                 $13, <array>
         15      > JMPZ                                                     ~14, ->17
         16    > > JMP                                                      ->11
   22    17    >   INIT_METHOD_CALL                                         !3, 'isDir'
         18        DO_FCALL                                      0  $15     
         19      > JMPZ                                                     $15, ->25
         20    >   INIT_METHOD_CALL                                         !3, 'getFilename'
         21        DO_FCALL                                      0  $16     
         22        INIT_ARRAY                                       ~17     <array>, $16
         23        QM_ASSIGN                                        ~18     ~17
         24      > JMP                                                      ->29
         25    >   INIT_METHOD_CALL                                         !3, 'getFilename'
         26        DO_FCALL                                      0  $19     
         27        INIT_ARRAY                                       ~20     $19
         28        QM_ASSIGN                                        ~18     ~20
         29    >   ASSIGN                                                   !4, ~18
   23    30        INIT_METHOD_CALL                                         !2, 'getDepth'
         31        DO_FCALL                                      0  $22     
         32        SUB                                              ~23     $22, 1
         33        ASSIGN                                                   !5, ~23
         34      > JMP                                                      ->45
   24    35    >   INIT_METHOD_CALL                                         !2, 'getSubIterator'
         36        SEND_VAR_EX                                              !5
         37        DO_FCALL                                      0  $25     
         38        INIT_METHOD_CALL                                         $25, 'current'
         39        DO_FCALL                                      0  $26     
         40        INIT_METHOD_CALL                                         $26, 'getFilename'
         41        DO_FCALL                                      0  $27     
         42        INIT_ARRAY                                       ~28     !4, $27
         43        ASSIGN                                                   !4, ~28
   23    44        PRE_DEC                                                  !5
         45    >   IS_SMALLER_OR_EQUAL                                      0, !5
         46      > JMPNZ                                                    ~31, ->35
   26    47    >   INIT_FCALL                                               'array_merge_recursive'
         48        SEND_VAR                                                 !1
         49        SEND_VAR                                                 !4
         50        DO_ICALL                                         $32     
         51        ASSIGN                                                   !1, $32
   20    52      > JMP                                                      ->11
         53    >   FE_FREE                                                  $12
   28    54      > RETURN                                                   !1
   29    55*     > RETURN                                                   null

End of function dirtoarray2

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.12 ms | 1408 KiB | 21 Q