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++) { $node = $tree; while (!empty($stack) && $stack[count($stack) - 1]['DEPTH_LEVEL'] >= $arr[$i]['DEPTH_LEVEL']) { array_pop($stack); } foreach ($stack as $ancestor) { $node = $node[$ancestor['INDEX']]['CHILDREN']; } $node[] = [ 'ID' => $arr[$i]['ID'], 'NAME' => $arr[$i]['NAME'] ]; $stack[] = [ 'INDEX' => count($node) - 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 = 47
Branch analysis from position: 47
2 jumps found. (Code = 44) Position 1 = 50, Position 2 = 5
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
2 jumps found. (Code = 46) Position 1 = 13, Position 2 = 21
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 7
Branch analysis from position: 22
2 jumps found. (Code = 77) Position 1 = 23, Position 2 = 29
Branch analysis from position: 23
2 jumps found. (Code = 78) Position 1 = 24, Position 2 = 29
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 50, Position 2 = 5
Branch analysis from position: 50
Branch analysis from position: 5
Branch analysis from position: 29
Branch analysis from position: 7
2 jumps found. (Code = 46) Position 1 = 13, Position 2 = 21
Branch analysis from position: 13
Branch analysis from position: 21
Branch analysis from position: 21
filename:       /in/tr1s3
function name:  (null)
number of ops:  54
compiled vars:  !0 = $arr, !1 = $tree, !2 = $stack, !3 = $i, !4 = $node, !5 = $ancestor
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                                                      ->47
   22     5    >   ASSIGN                                                   !4, !1
   24     6      > JMP                                                      ->10
   25     7    >   INIT_FCALL                                               'array_pop'
          8        SEND_REF                                                 !2
          9        DO_ICALL                                                 
   24    10    >   ISSET_ISEMPTY_CV                                 ~12     !2
         11        BOOL_NOT                                         ~13     ~12
         12      > JMPZ_EX                                          ~13     ~13, ->21
         13    >   COUNT                                            ~14     !2
         14        SUB                                              ~15     ~14, 1
         15        FETCH_DIM_R                                      ~16     !2, ~15
         16        FETCH_DIM_R                                      ~17     ~16, 'DEPTH_LEVEL'
         17        FETCH_DIM_R                                      ~18     !0, !3
         18        FETCH_DIM_R                                      ~19     ~18, 'DEPTH_LEVEL'
         19        IS_SMALLER_OR_EQUAL                              ~20     ~19, ~17
         20        BOOL                                             ~13     ~20
         21    > > JMPNZ                                                    ~13, ->7
   28    22    > > FE_RESET_R                                       $21     !2, ->29
         23    > > FE_FETCH_R                                               $21, !5, ->29
   29    24    >   FETCH_DIM_R                                      ~22     !5, 'INDEX'
         25        FETCH_DIM_R                                      ~23     !4, ~22
         26        FETCH_DIM_R                                      ~24     ~23, 'CHILDREN'
         27        ASSIGN                                                   !4, ~24
   28    28      > JMP                                                      ->23
         29    >   FE_FREE                                                  $21
   33    30        FETCH_DIM_R                                      ~27     !0, !3
         31        FETCH_DIM_R                                      ~28     ~27, 'ID'
         32        INIT_ARRAY                                       ~29     ~28, 'ID'
   34    33        FETCH_DIM_R                                      ~30     !0, !3
         34        FETCH_DIM_R                                      ~31     ~30, 'NAME'
         35        ADD_ARRAY_ELEMENT                                ~29     ~31, 'NAME'
   32    36        ASSIGN_DIM                                               !4
   34    37        OP_DATA                                                  ~29
   38    38        COUNT                                            ~33     !4
         39        SUB                                              ~34     ~33, 1
         40        INIT_ARRAY                                       ~35     ~34, 'INDEX'
   39    41        FETCH_DIM_R                                      ~36     !0, !3
         42        FETCH_DIM_R                                      ~37     ~36, 'DEPTH_LEVEL'
         43        ADD_ARRAY_ELEMENT                                ~35     ~37, 'DEPTH_LEVEL'
   37    44        ASSIGN_DIM                                               !2
   39    45        OP_DATA                                                  ~35
   21    46        PRE_INC                                                  !3
         47    >   COUNT                                            ~39     !0
         48        IS_SMALLER                                               !3, ~39
         49      > JMPNZ                                                    ~40, ->5
   43    50    >   INIT_FCALL                                               'print_r'
         51        SEND_VAR                                                 !1
         52        DO_ICALL                                                 
         53      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
137.78 ms | 1004 KiB | 15 Q