3v4l.org

run code in 300+ PHP versions simultaneously
<?php $indexes = [ 26 => ["namespace" => "X\\Y\\Zed"], 9 => ["namespace" => "A\\B\\See"], 3 => ["namespace" => "A\\B\\Bee"], 38 => ["namespace" => "A\\B\\C\\Dee"], 51 => ["namespace" => "X\\Wye"], 16 => ["namespace" => "A\\Sea"], 12 => ["namespace" => "A\\Bees"], 31 => ["namespace" => "M"] ]; function foldersBeforeFiles($indexes){ $hierarchical_data = createHierarchicalStructure($indexes); fileSystemSorting($hierarchical_data); return associateKeys(flattenFileSystemResults($hierarchical_data),$indexes); } function associateKeys($data,$indexes){ $map = array_combine(array_column($indexes,'namespace'),array_keys($indexes)); $result = []; foreach($data as $val){ $result[ $map[$val] ] = ['namespace' => $val]; } return $result; } function flattenFileSystemResults($hierarchical_data){ $result = []; foreach($hierarchical_data as $key => $value){ if(count($value) > 0){ $sub_result = flattenFileSystemResults($value); foreach($sub_result as $r){ $result[] = $key . "\\" . $r; } }else{ $result[] = $key; } } return $result; } function fileSystemSorting(&$indexes){ foreach($indexes as $key => &$value){ fileSystemSorting($value); } uksort($indexes,function($key1,$key2) use ($indexes){ if(count($indexes[$key1]) == 0 && count($indexes[$key2]) > 0) return 1; if(count($indexes[$key2]) == 0 && count($indexes[$key1]) > 0) return -1; return strnatcmp($key1,$key2); }); } function createHierarchicalStructure($indexes){ $data = []; foreach($indexes as $d){ $temp = &$data; foreach(explode("\\",$d['namespace']) as $namespace){ if(!isset($temp[$namespace])){ $temp[$namespace] = []; } $temp = &$temp[$namespace]; } } return $data; } print_r(foldersBeforeFiles($indexes));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cvoB2
function name:  (null)
number of ops:  8
compiled vars:  !0 = $indexes
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   75     1        INIT_FCALL                                               'print_r'
          2        INIT_FCALL                                               'foldersbeforefiles'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function foldersbeforefiles:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cvoB2
function name:  foldersBeforeFiles
number of ops:  17
compiled vars:  !0 = $indexes, !1 = $hierarchical_data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
   17     1        INIT_FCALL_BY_NAME                                       'createHierarchicalStructure'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $2      
          4        ASSIGN                                                   !1, $2
   18     5        INIT_FCALL_BY_NAME                                       'fileSystemSorting'
          6        SEND_VAR_EX                                              !1
          7        DO_FCALL                                      0          
   19     8        INIT_FCALL_BY_NAME                                       'associateKeys'
          9        INIT_FCALL_BY_NAME                                       'flattenFileSystemResults'
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0  $5      
         12        SEND_VAR_NO_REF_EX                                       $5
         13        SEND_VAR_EX                                              !0
         14        DO_FCALL                                      0  $6      
         15      > RETURN                                                   $6
   20    16*     > RETURN                                                   null

End of function foldersbeforefiles

Function associatekeys:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 22
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 22
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
filename:       /in/cvoB2
function name:  associateKeys
number of ops:  25
compiled vars:  !0 = $data, !1 = $indexes, !2 = $map, !3 = $result, !4 = $val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   24     2        INIT_FCALL                                               'array_combine'
          3        INIT_FCALL                                               'array_column'
          4        SEND_VAR                                                 !1
          5        SEND_VAL                                                 'namespace'
          6        DO_ICALL                                         $5      
          7        SEND_VAR                                                 $5
          8        INIT_FCALL                                               'array_keys'
          9        SEND_VAR                                                 !1
         10        DO_ICALL                                         $6      
         11        SEND_VAR                                                 $6
         12        DO_ICALL                                         $7      
         13        ASSIGN                                                   !2, $7
   25    14        ASSIGN                                                   !3, <array>
   26    15      > FE_RESET_R                                       $10     !0, ->22
         16    > > FE_FETCH_R                                               $10, !4, ->22
   27    17    >   FETCH_DIM_R                                      ~11     !2, !4
         18        INIT_ARRAY                                       ~13     !4, 'namespace'
         19        ASSIGN_DIM                                               !3, ~11
         20        OP_DATA                                                  ~13
   26    21      > JMP                                                      ->16
         22    >   FE_FREE                                                  $10
   29    23      > RETURN                                                   !3
   30    24*     > RETURN                                                   null

End of function associatekeys

Function flattenfilesystemresults:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 24
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 24
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 21
Branch analysis from position: 8
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 19
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 19
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 19
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/cvoB2
function name:  flattenFileSystemResults
number of ops:  27
compiled vars:  !0 = $hierarchical_data, !1 = $result, !2 = $value, !3 = $key, !4 = $sub_result, !5 = $r
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
   33     1        ASSIGN                                                   !1, <array>
   34     2      > FE_RESET_R                                       $7      !0, ->24
          3    > > FE_FETCH_R                                       ~8      $7, !2, ->24
          4    >   ASSIGN                                                   !3, ~8
   35     5        COUNT                                            ~10     !2
          6        IS_SMALLER                                               0, ~10
          7      > JMPZ                                                     ~11, ->21
   36     8    >   INIT_FCALL_BY_NAME                                       'flattenFileSystemResults'
          9        SEND_VAR_EX                                              !2
         10        DO_FCALL                                      0  $12     
         11        ASSIGN                                                   !4, $12
   37    12      > FE_RESET_R                                       $14     !4, ->19
         13    > > FE_FETCH_R                                               $14, !5, ->19
   38    14    >   CONCAT                                           ~16     !3, '%5C'
         15        CONCAT                                           ~17     ~16, !5
         16        ASSIGN_DIM                                               !1
         17        OP_DATA                                                  ~17
   37    18      > JMP                                                      ->13
         19    >   FE_FREE                                                  $14
   35    20      > JMP                                                      ->23
   41    21    >   ASSIGN_DIM                                               !1
         22        OP_DATA                                                  !3
   34    23    > > JMP                                                      ->3
         24    >   FE_FREE                                                  $7
   45    25      > RETURN                                                   !1
   46    26*     > RETURN                                                   null

End of function flattenfilesystemresults

Function filesystemsorting:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 2, Position 2 = 8
Branch analysis from position: 2
2 jumps found. (Code = 126) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/cvoB2
function name:  fileSystemSorting
number of ops:  16
compiled vars:  !0 = $indexes, !1 = $value, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
   49     1      > FE_RESET_RW                                      $3      !0, ->8
          2    > > FE_FETCH_RW                                      ~4      $3, !1, ->8
          3    >   ASSIGN                                                   !2, ~4
   50     4        INIT_FCALL_BY_NAME                                       'fileSystemSorting'
          5        SEND_VAR_EX                                              !1
          6        DO_FCALL                                      0          
   49     7      > JMP                                                      ->2
          8    >   FE_FREE                                                  $3
   53     9        INIT_FCALL                                               'uksort'
         10        SEND_REF                                                 !0
         11        DECLARE_LAMBDA_FUNCTION                          ~7      [0]
         12        BIND_LEXICAL                                             ~7, !0
   57    13        SEND_VAL                                                 ~7
   53    14        DO_ICALL                                                 
   58    15      > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 13
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 46) Position 1 = 17, Position 2 = 21
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 23
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
Branch analysis from position: 11
filename:       /in/cvoB2
function name:  {closure}
number of ops:  29
compiled vars:  !0 = $key1, !1 = $key2, !2 = $indexes
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   53     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
   54     3        FETCH_DIM_R                                      ~3      !2, !0
          4        COUNT                                            ~4      ~3
          5        IS_EQUAL                                         ~5      ~4, 0
          6      > JMPZ_EX                                          ~5      ~5, ->11
          7    >   FETCH_DIM_R                                      ~6      !2, !1
          8        COUNT                                            ~7      ~6
          9        IS_SMALLER                                       ~8      0, ~7
         10        BOOL                                             ~5      ~8
         11    > > JMPZ                                                     ~5, ->13
         12    > > RETURN                                                   1
   55    13    >   FETCH_DIM_R                                      ~9      !2, !1
         14        COUNT                                            ~10     ~9
         15        IS_EQUAL                                         ~11     ~10, 0
         16      > JMPZ_EX                                          ~11     ~11, ->21
         17    >   FETCH_DIM_R                                      ~12     !2, !0
         18        COUNT                                            ~13     ~12
         19        IS_SMALLER                                       ~14     0, ~13
         20        BOOL                                             ~11     ~14
         21    > > JMPZ                                                     ~11, ->23
         22    > > RETURN                                                   -1
   56    23    >   INIT_FCALL                                               'strnatcmp'
         24        SEND_VAR                                                 !0
         25        SEND_VAR                                                 !1
         26        DO_ICALL                                         $15     
         27      > RETURN                                                   $15
   57    28*     > RETURN                                                   null

End of Dynamic Function 0

End of function filesystemsorting

Function createhierarchicalstructure:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 22
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 22
Branch analysis from position: 4
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 20
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 20
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 17
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 17
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 20
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
filename:       /in/cvoB2
function name:  createHierarchicalStructure
number of ops:  25
compiled vars:  !0 = $indexes, !1 = $data, !2 = $d, !3 = $temp, !4 = $namespace
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   RECV                                             !0      
   61     1        ASSIGN                                                   !1, <array>
   62     2      > FE_RESET_R                                       $6      !0, ->22
          3    > > FE_FETCH_R                                               $6, !2, ->22
   63     4    >   ASSIGN_REF                                               !3, !1
   64     5        INIT_FCALL                                               'explode'
          6        SEND_VAL                                                 '%5C'
          7        FETCH_DIM_R                                      ~8      !2, 'namespace'
          8        SEND_VAL                                                 ~8
          9        DO_ICALL                                         $9      
         10      > FE_RESET_R                                       $10     $9, ->20
         11    > > FE_FETCH_R                                               $10, !4, ->20
   65    12    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~11     !3, !4
         13        BOOL_NOT                                         ~12     ~11
         14      > JMPZ                                                     ~12, ->17
   66    15    >   ASSIGN_DIM                                               !3, !4
         16        OP_DATA                                                  <array>
   68    17    >   FETCH_DIM_W                                      $14     !3, !4
         18        ASSIGN_REF                                               !3, $14
   64    19      > JMP                                                      ->11
         20    >   FE_FREE                                                  $10
   62    21      > JMP                                                      ->3
         22    >   FE_FREE                                                  $6
   72    23      > RETURN                                                   !1
   73    24*     > RETURN                                                   null

End of function createhierarchicalstructure

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.33 ms | 1026 KiB | 21 Q