3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getTree($array) { $level = 0; $tree = []; $stack = [uniqid(1) => &$tree]; //why uniqid? Try do debug without that :p 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[uniqid()] = &$stack[$top][key($stack[$top])]; } //add ['children'] dim to top stack element end($stack); $top = key($stack); $stack[$top]['children'] = []; $stack[uniqid(1)] = &$stack[$top]['children']; //add current item as a first child of top stack element end($stack); $top = key($stack); $stack[$top][] = $item; } elseif($item['level']<$level) //pop stack items till corresponding level { while($item['level']<$level--) { //two times: one for last pointer, one for ['children'] dim array_pop($stack); array_pop($stack); } end($stack); $stack[key($stack)][] = $item; } elseif($item['level']==$level) //add to current level { //just add since top stack element will be ['children'] dim $stack[key($stack)][] = $item; } end($stack); $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'] ]; echo('<pre>'); $result = getTree($array); print_r($result);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rKtco
function name:  (null)
number of ops:  10
compiled vars:  !0 = $array, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   ASSIGN                                                   !0, <array>
   59     1        ECHO                                                     '%3Cpre%3E'
   60     2        INIT_FCALL                                               'gettree'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $3      
          5        ASSIGN                                                   !1, $3
   61     6        INIT_FCALL                                               'print_r'
          7        SEND_VAR                                                 !1
          8        DO_ICALL                                                 
          9      > RETURN                                                   1

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

End of function gettree

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.3 ms | 1407 KiB | 24 Q