3v4l.org

run code in 500+ PHP versions simultaneously
<?php function getTree(array $array) { $level = 0; $tree = []; $stack = [&$tree]; foreach($array as $item) { if($item['level']>$level) //expand stack for new items { //if there are child elements, add last to stack: $top = key($stack); if(count($stack[$top])) { end($stack[$top]); $stack[] = &$stack[$top][key($stack[$top])]; } //add ['children'] dim to top stack element end($stack); $top = key($stack); $stack[$top]['children'] = []; $stack[] = &$stack[$top]['children']; } while($item['level']<$level--) //pop till certain level { //two times: one for last pointer, one for ['children'] dim array_pop($stack); array_pop($stack); } //add item to stack top: end($stack); $stack[key($stack)][] = $item; $level = $item['level']; } return $tree; } $array = [ ['level'=>1, 'name' => 'Root #1'], ['level'=>1, 'name' => 'Root #2'], ['level'=>2, 'name' => 'subroot 2-1'], ['level'=>3, 'name' => '__subroot 2-1/1'], ['level'=>2, 'name' => 'subroot 2-2'], ['level'=>1, 'name' => 'Root #3'] ]; print_r(getTree($array));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ofhSD
function name:  (null)
number of ops:  8
compiled vars:  !0 = $array
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   39     0  E >   ASSIGN                                                       !0, <array>
   48     1        INIT_FCALL                                                   'print_r'
          2        INIT_FCALL                                                   'gettree'
          3        SEND_VAR                                                     !0
          4        DO_FCALL                                          0  $2      
          5        SEND_VAR                                                     $2
          6        DO_ICALL                                                     
          7      > RETURN                                                       1

Function gettree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 68
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 68
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 45
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 30
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
2 jumps found. (Code = 44) Position 1 = 56, Position 2 = 46
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 46
2 jumps found. (Code = 44) Position 1 = 56, Position 2 = 46
Branch analysis from position: 56
Branch analysis from position: 46
Branch analysis from position: 30
Branch analysis from position: 45
Branch analysis from position: 68
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 68
filename:       /in/ofhSD
function name:  getTree
number of ops:  71
compiled vars:  !0 = $array, !1 = $level, !2 = $tree, !3 = $stack, !4 = $item, !5 = $top
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
    5     1        ASSIGN                                                       !1, 0
    6     2        ASSIGN                                                       !2, <array>
    7     3        INIT_ARRAY                                           ~8      !2
          4        ASSIGN                                                       !3, ~8
    8     5      > FE_RESET_R                                           $10     !0, ->68
          6    > > FE_FETCH_R                                                   $10, !4, ->68
   10     7    >   FETCH_DIM_R                                          ~11     !4, 'level'
          8        IS_SMALLER                                                   !1, ~11
          9      > JMPZ                                                         ~12, ->45
   13    10    >   INIT_FCALL                                                   'key'
         11        SEND_VAR                                                     !3
         12        DO_ICALL                                             $13     
         13        ASSIGN                                                       !5, $13
   14    14        FETCH_DIM_R                                          ~15     !3, !5
         15        COUNT                                                ~16     ~15
         16      > JMPZ                                                         ~16, ->30
   16    17    >   INIT_FCALL                                                   'end'
         18        FETCH_DIM_W                                          $17     !3, !5
         19        SEND_REF                                                     $17
         20        DO_ICALL                                                     
   17    21        INIT_FCALL                                                   'key'
         22        FETCH_DIM_R                                          ~21     !3, !5
         23        SEND_VAL                                                     ~21
         24        DO_ICALL                                             $22     
         25        FETCH_DIM_W                                          $20     !3, !5
         26        FETCH_DIM_W                                          $23     $20, $22
         27        MAKE_REF                                             $24     $23
         28        FETCH_DIM_W                                          $19     !3
         29        ASSIGN_REF                                                   $19, $24
   20    30    >   INIT_FCALL                                                   'end'
         31        SEND_REF                                                     !3
         32        DO_ICALL                                                     
   21    33        INIT_FCALL                                                   'key'
         34        SEND_VAR                                                     !3
         35        DO_ICALL                                             $27     
         36        ASSIGN                                                       !5, $27
   22    37        FETCH_DIM_W                                          $29     !3, !5
         38        ASSIGN_DIM                                                   $29, 'children'
         39        OP_DATA                                                      <array>
   23    40        FETCH_DIM_W                                          $32     !3, !5
         41        FETCH_DIM_W                                          $33     $32, 'children'
         42        MAKE_REF                                             $34     $33
         43        FETCH_DIM_W                                          $31     !3
         44        ASSIGN_REF                                                   $31, $34
   25    45    > > JMP                                                          ->52
   28    46    >   INIT_FCALL                                                   'array_pop'
         47        SEND_REF                                                     !3
         48        DO_ICALL                                                     
   29    49        INIT_FCALL                                                   'array_pop'
         50        SEND_REF                                                     !3
         51        DO_ICALL                                                     
   25    52    >   FETCH_DIM_R                                          ~38     !4, 'level'
         53        POST_DEC                                             ~39     !1
         54        IS_SMALLER                                                   ~38, ~39
         55      > JMPNZ                                                        ~40, ->46
   32    56    >   INIT_FCALL                                                   'end'
         57        SEND_REF                                                     !3
         58        DO_ICALL                                                     
   33    59        INIT_FCALL                                                   'key'
         60        SEND_VAR                                                     !3
         61        DO_ICALL                                             $42     
         62        FETCH_DIM_W                                          $43     !3, $42
         63        ASSIGN_DIM                                                   $43
         64        OP_DATA                                                      !4
   34    65        FETCH_DIM_R                                          ~45     !4, 'level'
         66        ASSIGN                                                       !1, ~45
    8    67      > JMP                                                          ->6
         68    >   FE_FREE                                                      $10
   36    69      > RETURN                                                       !2
   37    70*     > RETURN                                                       null

End of function gettree

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
178.8 ms | 3927 KiB | 18 Q