3v4l.org

run code in 500+ PHP versions simultaneously
<?php function pathToNestedArray($path, $separator = "/") { // use DIRECTORY_SEPARATOR for greater utility $halves = explode($separator, $path, 2); // maximum explosion is 2 elements if (!isset($halves[1])) return [$path]; // no folder to nest into, append as indexed element (Done) return [$halves[0] => pathToNestedArray($halves[1])]; // apply level's key value and recurse } $paths = [ "fileZ.php", "folder1/content/file1.php", "folder1/content/file2.php", "folder1/edit/file1.php", "folder1/edit/file2.php", "folder1/pagination/file1.php", "folder1/pagination/file2.php", "folder1/toolbar/file1.php", "folder1/toolbar/file2.php", "folder2/cms/html/file1.php", "folder2/cms/html/file2.php" ]; $result = []; foreach ($paths as $path) { $result = array_merge_recursive($result, pathToNestedArray($path)); } var_export($result);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 13
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 13
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/m1XLJ
function name:  (null)
number of ops:  18
compiled vars:  !0 = $paths, !1 = $result, !2 = $path
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    8     0  E >   ASSIGN                                                       !0, <array>
   22     1        ASSIGN                                                       !1, <array>
   23     2      > FE_RESET_R                                           $5      !0, ->13
          3    > > FE_FETCH_R                                                   $5, !2, ->13
   24     4    >   INIT_FCALL                                                   'array_merge_recursive'
          5        SEND_VAR                                                     !1
          6        INIT_FCALL                                                   'pathtonestedarray'
          7        SEND_VAR                                                     !2
          8        DO_FCALL                                          0  $6      
          9        SEND_VAR                                                     $6
         10        DO_ICALL                                             $7      
         11        ASSIGN                                                       !1, $7
   23    12      > JMP                                                          ->3
         13    >   FE_FREE                                                      $5
   26    14        INIT_FCALL                                                   'var_export'
         15        SEND_VAR                                                     !1
         16        DO_ICALL                                                     
         17      > RETURN                                                       1

Function pathtonestedarray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 13
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/m1XLJ
function name:  pathToNestedArray
number of ops:  22
compiled vars:  !0 = $path, !1 = $separator, !2 = $halves
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    2     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      '%2F'
    3     2        INIT_FCALL                                                   'explode'
          3        SEND_VAR                                                     !1
          4        SEND_VAR                                                     !0
          5        SEND_VAL                                                     2
          6        DO_ICALL                                             $3      
          7        ASSIGN                                                       !2, $3
    4     8        ISSET_ISEMPTY_DIM_OBJ                             0  ~5      !2, 1
          9        BOOL_NOT                                             ~6      ~5
         10      > JMPZ                                                         ~6, ->13
         11    >   INIT_ARRAY                                           ~7      !0
         12      > RETURN                                                       ~7
    5    13    >   FETCH_DIM_R                                          ~8      !2, 0
         14        INIT_FCALL_BY_NAME                                           'pathToNestedArray'
         15        CHECK_FUNC_ARG                                               
         16        FETCH_DIM_FUNC_ARG                                   $9      !2, 1
         17        SEND_FUNC_ARG                                                $9
         18        DO_FCALL                                          0  $10     
         19        INIT_ARRAY                                           ~11     $10, ~8
         20      > RETURN                                                       ~11
    6    21*     > RETURN                                                       null

End of function pathtonestedarray

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
159.12 ms | 2357 KiB | 16 Q