3v4l.org

run code in 300+ PHP versions simultaneously
<?php function &buildTreeHelper(&$array, $currentLevel = 1) { $result = array(); $lastIndex = 0; while($pair = each($array)) { list(, $row) = $pair; $level = $row['level']; if ($level > $currentLevel) { $result[$lastIndex]['children'] = &buildTreeHelper($array, $level); } else if ($level == $currentLevel) { $result[++$lastIndex] = $row; } else { prev($array); // shift back break; } } return $result; } function buildTree($array) { reset($array); return buildTreeHelper($array); } $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'=>3, 'name' => '__subroot 2-1/2'], ['level'=>4, 'name' => '__subroot 2-2/1'], ['level'=>2, 'name' => 'subroot 2-2'], ['level'=>2, 'name' => 'subroot 2-3'], ['level'=>1, 'name' => 'Root #3'] ]; $time = microtime(true); print_r(buildTree($array)); echo round(microtime(true) - $time, 10);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ll429
function name:  (null)
number of ops:  21
compiled vars:  !0 = $array, !1 = $time
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   ASSIGN                                                   !0, <array>
   41     1        INIT_FCALL                                               'microtime'
          2        SEND_VAL                                                 <true>
          3        DO_ICALL                                         $3      
          4        ASSIGN                                                   !1, $3
   42     5        INIT_FCALL                                               'print_r'
          6        INIT_FCALL                                               'buildtree'
          7        SEND_VAR                                                 !0
          8        DO_FCALL                                      0  $5      
          9        SEND_VAR                                                 $5
         10        DO_ICALL                                                 
   43    11        INIT_FCALL                                               'round'
         12        INIT_FCALL                                               'microtime'
         13        SEND_VAL                                                 <true>
         14        DO_ICALL                                         $7      
         15        SUB                                              ~8      $7, !1
         16        SEND_VAL                                                 ~8
         17        SEND_VAL                                                 10
         18        DO_ICALL                                         $9      
         19        ECHO                                                     $9
         20      > RETURN                                                   1

Function buildtreehelper:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
2 jumps found. (Code = 44) Position 1 = 37, Position 2 = 5
Branch analysis from position: 37
Return found
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 22
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 28
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
filename:       /in/ll429
function name:  buildTreeHelper
number of ops:  39
compiled vars:  !0 = $array, !1 = $currentLevel, !2 = $result, !3 = $lastIndex, !4 = $pair, !5 = $row, !6 = $level
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      1
    5     2        ASSIGN                                                   !2, <array>
    6     3        ASSIGN                                                   !3, 0
    7     4      > JMP                                                      ->32
    8     5    >   QM_ASSIGN                                        ~9      !4
          6        FETCH_LIST_R                                     $10     ~9, 1
          7        ASSIGN                                                   !5, $10
          8        FREE                                                     ~9
    9     9        FETCH_DIM_R                                      ~12     !5, 'level'
         10        ASSIGN                                                   !6, ~12
   10    11        IS_SMALLER                                               !1, !6
         12      > JMPZ                                                     ~14, ->22
   11    13    >   INIT_FCALL_BY_NAME                                       'buildTreeHelper'
         14        SEND_VAR_EX                                              !0
         15        SEND_VAR_EX                                              !6
         16        DO_FCALL                                      0  $17     
         17        MAKE_REF                                         $18     $17
         18        FETCH_DIM_W                                      $15     !2, !3
         19        FETCH_DIM_W                                      $16     $15, 'children'
         20        ASSIGN_REF                                               $16, $18
         21      > JMP                                                      ->32
   12    22    >   IS_EQUAL                                                 !6, !1
         23      > JMPZ                                                     ~20, ->28
   13    24    >   PRE_INC                                          ~21     !3
         25        ASSIGN_DIM                                               !2, ~21
         26        OP_DATA                                                  !5
         27      > JMP                                                      ->32
   15    28    >   INIT_FCALL                                               'prev'
         29        SEND_REF                                                 !0
         30        DO_ICALL                                                 
   16    31      > JMP                                                      ->37
    7    32    >   INIT_FCALL_BY_NAME                                       'each'
         33        SEND_VAR_EX                                              !0
         34        DO_FCALL                                      0  $24     
         35        ASSIGN                                           ~25     !4, $24
         36      > JMPNZ                                                    ~25, ->5
   19    37    > > RETURN_BY_REF                                            !2
   20    38*     > RETURN_BY_REF                                            null

End of function buildtreehelper

Function buildtree:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ll429
function name:  buildTree
number of ops:  9
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
   24     1        INIT_FCALL                                               'reset'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                                 
   25     4        INIT_FCALL                                               'buildtreehelper'
          5        SEND_REF                                                 !0
          6        DO_FCALL                                      0  $2      
          7      > RETURN                                                   $2
   26     8*     > RETURN                                                   null

End of function buildtree

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.37 ms | 1403 KiB | 25 Q