3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = array( array('ID' => 705, 'NAME' => 'Телефоны и гаджеты', 'DEPTH_LEVEL' => 1), array('ID' => 706, 'NAME' => 'Смартфоны', 'DEPTH_LEVEL' => 2), array('ID' => 5586, 'NAME' => 'Аксессуары для гаджетов', 'DEPTH_LEVEL' => 2), array('ID' => 715, 'NAME' => 'Аксессуары для телефонов', 'DEPTH_LEVEL' => 3), array('ID' => 716, 'NAME' => 'Чехлы для смартфонов', 'DEPTH_LEVEL' => 4), array('ID' => 5536, 'NAME' => 'Чехлы для Apple', 'DEPTH_LEVEL' => 5), array('ID' => 5539, 'NAME' => 'Чехлы для Samsung', 'DEPTH_LEVEL' => 5), array('ID' => 3010, 'NAME' => 'Защитные стекла для телефонов', 'DEPTH_LEVEL' => 3), array('ID' => 660, 'NAME' => 'Компьютерная техника', 'DEPTH_LEVEL' => 1) ); $tree = []; $stack = []; for ($i = 0; $i < count($arr); $i++) { while (!empty($stack) && $stack[count($stack) - 1]['DEPTH_LEVEL'] >= $arr[$i]['DEPTH_LEVEL']) { array_pop($stack); } $index=end($stack)['INDEX']; $tree[$index]['CHILDREN'][] = [ 'ID' => $arr[$i]['ID'], 'NAME' => $arr[$i]['NAME'] ]; $stack[] = [ 'INDEX' => count($tree[$index]) - 1, 'DEPTH_LEVEL' => $arr[$i]['DEPTH_LEVEL'], ]; } print_r($tree);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 5
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
2 jumps found. (Code = 46) Position 1 = 12, Position 2 = 20
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 6
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 5
Branch analysis from position: 49
Branch analysis from position: 5
Branch analysis from position: 6
2 jumps found. (Code = 46) Position 1 = 12, Position 2 = 20
Branch analysis from position: 12
Branch analysis from position: 20
Branch analysis from position: 20
filename:       /in/GgjoI
function name:  (null)
number of ops:  53
compiled vars:  !0 = $arr, !1 = $tree, !2 = $stack, !3 = $i, !4 = $index
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   17     1        ASSIGN                                                   !1, <array>
   18     2        ASSIGN                                                   !2, <array>
   21     3        ASSIGN                                                   !3, 0
          4      > JMP                                                      ->46
   22     5    > > JMP                                                      ->9
   23     6    >   INIT_FCALL                                               'array_pop'
          7        SEND_REF                                                 !2
          8        DO_ICALL                                                 
   22     9    >   ISSET_ISEMPTY_CV                                 ~10     !2
         10        BOOL_NOT                                         ~11     ~10
         11      > JMPZ_EX                                          ~11     ~11, ->20
         12    >   COUNT                                            ~12     !2
         13        SUB                                              ~13     ~12, 1
         14        FETCH_DIM_R                                      ~14     !2, ~13
         15        FETCH_DIM_R                                      ~15     ~14, 'DEPTH_LEVEL'
         16        FETCH_DIM_R                                      ~16     !0, !3
         17        FETCH_DIM_R                                      ~17     ~16, 'DEPTH_LEVEL'
         18        IS_SMALLER_OR_EQUAL                              ~18     ~17, ~15
         19        BOOL                                             ~11     ~18
         20    > > JMPNZ                                                    ~11, ->6
   25    21    >   INIT_FCALL                                               'end'
         22        SEND_REF                                                 !2
         23        DO_ICALL                                         $19     
         24        FETCH_DIM_R                                      ~20     $19, 'INDEX'
         25        ASSIGN                                                   !4, ~20
   28    26        FETCH_DIM_R                                      ~25     !0, !3
         27        FETCH_DIM_R                                      ~26     ~25, 'ID'
         28        INIT_ARRAY                                       ~27     ~26, 'ID'
   29    29        FETCH_DIM_R                                      ~28     !0, !3
         30        FETCH_DIM_R                                      ~29     ~28, 'NAME'
         31        ADD_ARRAY_ELEMENT                                ~27     ~29, 'NAME'
   27    32        FETCH_DIM_W                                      $22     !1, !4
         33        FETCH_DIM_W                                      $23     $22, 'CHILDREN'
         34        ASSIGN_DIM                                               $23
   29    35        OP_DATA                                                  ~27
   33    36        FETCH_DIM_R                                      ~31     !1, !4
         37        COUNT                                            ~32     ~31
         38        SUB                                              ~33     ~32, 1
         39        INIT_ARRAY                                       ~34     ~33, 'INDEX'
   34    40        FETCH_DIM_R                                      ~35     !0, !3
         41        FETCH_DIM_R                                      ~36     ~35, 'DEPTH_LEVEL'
         42        ADD_ARRAY_ELEMENT                                ~34     ~36, 'DEPTH_LEVEL'
   32    43        ASSIGN_DIM                                               !2
   34    44        OP_DATA                                                  ~34
   21    45        PRE_INC                                                  !3
         46    >   COUNT                                            ~38     !0
         47        IS_SMALLER                                               !3, ~38
         48      > JMPNZ                                                    ~39, ->5
   38    49    >   INIT_FCALL                                               'print_r'
         50        SEND_VAR                                                 !1
         51        DO_ICALL                                                 
         52      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
168.3 ms | 1007 KiB | 16 Q