3v4l.org

run code in 500+ PHP versions simultaneously
<?php $inputArray = array ( 0 => array ( 'id_file' => '96', 'file_name' => 'Важный документ.docx', 'file_directory' => 'АК\\Документы', 'region' => 'Для работы', 'directory' => 'АК\\Документы\\Важный документ.docx', ), 1 => array ( 'id_file' => '97', 'file_name' => 'июнь_2020.docx', 'file_directory' => 'АК\\Документы', 'region' => 'Для работы', 'directory' => 'АК\\Документы\\июнь_2020.docx', ), 2 => array ( 'id_file' => '104', 'file_name' => '111222.pdf', 'file_directory' => 'АК\\Документы\\Почта', 'region' => 'Для работы', 'directory' => 'АК\\Документы\\Почта\\111222.pdf', ), 3 => array ( 'id_file' => '110', 'file_name' => '111222sss.pdf', 'file_directory' => 'АК\\Документы\\Почта', 'region' => 'Для работы', 'directory' => 'АК\\Документы\\Почта\\111222sss.pdf', ), 4 => array ( 'id_file' => '116', 'file_name' => 'asdasd1.pdf', 'file_directory' => 'АК\\Документы\\УК', 'region' => 'Для работы', 'directory' => 'АК\\Документы\\УК\\asdasd1.pdf', ), 5 => array ( 'id_file' => '128', 'file_name' => '111222sss.pdf', 'file_directory' => 'ДК\\Картинки\\УК', 'region' => 'Для отдыха', 'directory' => 'ДК\\Картинки\\УК\\111222sss.pdf', ), 6 => array ( 'id_file' => '128', 'file_name' => 'asdasd2.pdf', 'file_directory' => 'АК\\Приказы', 'region' => 'Для работы', 'directory' => 'АК\\Приказы\\asdasd2.pdf', ), ); $list = array_map(function($item){ $path = explode('\\', $item['file_directory']); array_splice($path, 1, 0, $item['region']); return array( 'id' => $item['id_file'], 'path' => $path, 'name' => $item['file_name'], ); }, $inputArray); function buildTree($items, $prefix = []) { $res = []; $visited = []; foreach($items as $item) { if (count($item['path']) < count($prefix)) continue; if (array_slice($item['path'], 0, count($prefix)) !== $prefix) continue; // echo json_encode([$prefix, $item], JSON_UNESCAPED_UNICODE), PHP_EOL; $node = []; if (count($item['path']) > count($prefix)) { $curPath = $item['path'][count($prefix)]; $nextPrefix = array_merge($prefix, [$curPath]); $strPrefix = implode('\\', $nextPrefix); if ($visited[$strPrefix] ?? false) continue; $node['type'] = 'folder'; $node['path'] = $strPrefix; $node['name'] = $curPath; $node['children'] = buildTree($items, $nextPrefix); $visited[$strPrefix] = true; } else { $node['type'] = 'file'; $node['name'] = $item['name']; } $res[] = $node; } return $res; } $tree = buildTree($list); // echo json_encode($list, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); echo json_encode($tree, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IoVaL
function name:  (null)
number of ops:  17
compiled vars:  !0 = $inputArray, !1 = $list, !2 = $tree
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
   62     1        INIT_FCALL                                                   'array_map'
          2        DECLARE_LAMBDA_FUNCTION                              ~4      [0]
   70     3        SEND_VAL                                                     ~4
          4        SEND_VAR                                                     !0
   62     5        DO_ICALL                                             $5      
          6        ASSIGN                                                       !1, $5
  100     7        INIT_FCALL                                                   'buildtree'
          8        SEND_VAR                                                     !1
          9        DO_FCALL                                          0  $7      
         10        ASSIGN                                                       !2, $7
  104    11        INIT_FCALL                                                   'json_encode'
         12        SEND_VAR                                                     !2
         13        SEND_VAL                                                     384
         14        DO_ICALL                                             $9      
         15        ECHO                                                         $9
         16      > RETURN                                                       1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IoVaL
function name:  {closure:/in/IoVaL:62}
number of ops:  21
compiled vars:  !0 = $item, !1 = $path
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   62     0  E >   RECV                                                 !0      
   63     1        INIT_FCALL                                                   'explode'
          2        SEND_VAL                                                     '%5C'
          3        FETCH_DIM_R                                          ~2      !0, 'file_directory'
          4        SEND_VAL                                                     ~2
          5        DO_ICALL                                             $3      
          6        ASSIGN                                                       !1, $3
   64     7        INIT_FCALL                                                   'array_splice'
          8        SEND_REF                                                     !1
          9        SEND_VAL                                                     1
         10        SEND_VAL                                                     0
         11        FETCH_DIM_R                                          ~5      !0, 'region'
         12        SEND_VAL                                                     ~5
         13        DO_ICALL                                                     
   66    14        FETCH_DIM_R                                          ~7      !0, 'id_file'
         15        INIT_ARRAY                                           ~8      ~7, 'id'
   67    16        ADD_ARRAY_ELEMENT                                    ~8      !1, 'path'
   68    17        FETCH_DIM_R                                          ~9      !0, 'file_name'
         18        ADD_ARRAY_ELEMENT                                    ~8      ~9, 'name'
         19      > RETURN                                                       ~8
   70    20*     > RETURN                                                       null

End of Dynamic Function 0

Function buildtree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 68
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 68
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 22
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 60
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 45
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 60
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 68
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 68
filename:       /in/IoVaL
function name:  buildTree
number of ops:  71
compiled vars:  !0 = $items, !1 = $prefix, !2 = $res, !3 = $visited, !4 = $item, !5 = $node, !6 = $curPath, !7 = $nextPrefix, !8 = $strPrefix
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   73     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      <array>
   74     2        ASSIGN                                                       !2, <array>
   75     3        ASSIGN                                                       !3, <array>
   76     4      > FE_RESET_R                                           $11     !0, ->68
          5    > > FE_FETCH_R                                                   $11, !4, ->68
   77     6    >   FETCH_DIM_R                                          ~12     !4, 'path'
          7        COUNT                                                ~13     ~12
          8        COUNT                                                ~14     !1
          9        IS_SMALLER                                                   ~13, ~14
         10      > JMPZ                                                         ~15, ->12
         11    > > JMP                                                          ->5
   78    12    >   INIT_FCALL                                                   'array_slice'
         13        FETCH_DIM_R                                          ~16     !4, 'path'
         14        SEND_VAL                                                     ~16
         15        SEND_VAL                                                     0
         16        COUNT                                                ~17     !1
         17        SEND_VAL                                                     ~17
         18        DO_ICALL                                             $18     
         19        IS_NOT_IDENTICAL                                             !1, $18
         20      > JMPZ                                                         ~19, ->22
         21    > > JMP                                                          ->5
   80    22    >   ASSIGN                                                       !5, <array>
   81    23        FETCH_DIM_R                                          ~21     !4, 'path'
         24        COUNT                                                ~22     ~21
         25        COUNT                                                ~23     !1
         26        IS_SMALLER                                                   ~23, ~22
         27      > JMPZ                                                         ~24, ->60
   82    28    >   COUNT                                                ~26     !1
         29        FETCH_DIM_R                                          ~25     !4, 'path'
         30        FETCH_DIM_R                                          ~27     ~25, ~26
         31        ASSIGN                                                       !6, ~27
   83    32        INIT_FCALL                                                   'array_merge'
         33        SEND_VAR                                                     !1
         34        INIT_ARRAY                                           ~29     !6
         35        SEND_VAL                                                     ~29
         36        DO_ICALL                                             $30     
         37        ASSIGN                                                       !7, $30
   84    38        FRAMELESS_ICALL_2                implode             ~32     '%5C', !7
         39        ASSIGN                                                       !8, ~32
   85    40        FETCH_DIM_IS                                         ~34     !3, !8
         41        COALESCE                                             ~35     ~34
         42        QM_ASSIGN                                            ~35     <false>
         43      > JMPZ                                                         ~35, ->45
         44    > > JMP                                                          ->5
   86    45    >   ASSIGN_DIM                                                   !5, 'type'
         46        OP_DATA                                                      'folder'
   87    47        ASSIGN_DIM                                                   !5, 'path'
         48        OP_DATA                                                      !8
   88    49        ASSIGN_DIM                                                   !5, 'name'
         50        OP_DATA                                                      !6
   89    51        INIT_FCALL_BY_NAME                                           'buildTree'
         52        SEND_VAR_EX                                                  !0
         53        SEND_VAR_EX                                                  !7
         54        DO_FCALL                                          0  $40     
         55        ASSIGN_DIM                                                   !5, 'children'
         56        OP_DATA                                                      $40
   90    57        ASSIGN_DIM                                                   !3, !8
         58        OP_DATA                                                      <true>
   81    59      > JMP                                                          ->65
   92    60    >   ASSIGN_DIM                                                   !5, 'type'
         61        OP_DATA                                                      'file'
   93    62        FETCH_DIM_R                                          ~44     !4, 'name'
         63        ASSIGN_DIM                                                   !5, 'name'
         64        OP_DATA                                                      ~44
   95    65    >   ASSIGN_DIM                                                   !2
         66        OP_DATA                                                      !5
   76    67      > JMP                                                          ->5
         68    >   FE_FREE                                                      $11
   97    69      > RETURN                                                       !2
   98    70*     > RETURN                                                       null

End of function buildtree

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
156.67 ms | 2551 KiB | 15 Q