3v4l.org

run code in 300+ 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                                  '%00%7Bclosure%7D%2Fin%2FIoVaL%3A62%240'
   70     3        SEND_VAL                                                 ~4
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $5      
   62     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

Function %00%7Bclosure%7D%2Fin%2FIoVaL%3A62%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IoVaL
function name:  {closure}
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 function %00%7Bclosure%7D%2Fin%2FIoVaL%3A62%240

Function buildtree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 71
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 71
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 = 63
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 48
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 48
1 jumps found. (Code = 42) Position 1 = 68
Branch analysis from position: 68
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 63
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 71
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 71
filename:       /in/IoVaL
function name:  buildTree
number of ops:  74
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, ->71
          5    > > FE_FETCH_R                                               $11, !4, ->71
   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, ->63
   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        INIT_FCALL                                               'implode'
         39        SEND_VAL                                                 '%5C'
         40        SEND_VAR                                                 !7
         41        DO_ICALL                                         $32     
         42        ASSIGN                                                   !8, $32
   85    43        FETCH_DIM_IS                                     ~34     !3, !8
         44        COALESCE                                         ~35     ~34
         45        QM_ASSIGN                                        ~35     <false>
         46      > JMPZ                                                     ~35, ->48
         47    > > JMP                                                      ->5
   86    48    >   ASSIGN_DIM                                               !5, 'type'
         49        OP_DATA                                                  'folder'
   87    50        ASSIGN_DIM                                               !5, 'path'
         51        OP_DATA                                                  !8
   88    52        ASSIGN_DIM                                               !5, 'name'
         53        OP_DATA                                                  !6
   89    54        INIT_FCALL_BY_NAME                                       'buildTree'
         55        SEND_VAR_EX                                              !0
         56        SEND_VAR_EX                                              !7
         57        DO_FCALL                                      0  $40     
         58        ASSIGN_DIM                                               !5, 'children'
         59        OP_DATA                                                  $40
   90    60        ASSIGN_DIM                                               !3, !8
         61        OP_DATA                                                  <true>
         62      > JMP                                                      ->68
   92    63    >   ASSIGN_DIM                                               !5, 'type'
         64        OP_DATA                                                  'file'
   93    65        FETCH_DIM_R                                      ~44     !4, 'name'
         66        ASSIGN_DIM                                               !5, 'name'
         67        OP_DATA                                                  ~44
   95    68    >   ASSIGN_DIM                                               !2
         69        OP_DATA                                                  !5
   76    70      > JMP                                                      ->5
         71    >   FE_FREE                                                  $11
   97    72      > RETURN                                                   !2
   98    73*     > RETURN                                                   null

End of function buildtree

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
171.99 ms | 1407 KiB | 28 Q