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); var_dump(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/s8OLS
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                                               'var_dump'
          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 = 31
Branch analysis from position: 31
2 jumps found. (Code = 44) Position 1 = 36, Position 2 = 5
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 21
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 27
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
filename:       /in/s8OLS
function name:  buildTreeHelper
number of ops:  38
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                                                      ->31
    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, ->21
   11    13    >   INIT_FCALL_BY_NAME                                       'buildTreeHelper'
         14        SEND_VAR_EX                                              !0
         15        SEND_VAR_EX                                              !6
         16        DO_FCALL                                      0  $17     
         17        FETCH_DIM_W                                      $15     !2, !3
         18        ASSIGN_DIM                                               $15, 'children'
         19        OP_DATA                                                  $17
         20      > JMP                                                      ->31
   12    21    >   IS_EQUAL                                                 !6, !1
         22      > JMPZ                                                     ~18, ->27
   13    23    >   PRE_INC                                          ~19     !3
         24        ASSIGN_DIM                                               !2, ~19
         25        OP_DATA                                                  !5
         26      > JMP                                                      ->31
   15    27    >   INIT_FCALL                                               'prev'
         28        SEND_REF                                                 !0
         29        DO_ICALL                                                 
   16    30      > JMP                                                      ->36
    7    31    >   INIT_FCALL_BY_NAME                                       'each'
         32        SEND_VAR_EX                                              !0
         33        DO_FCALL                                      0  $22     
         34        ASSIGN                                           ~23     !4, $22
         35      > JMPNZ                                                    ~23, ->5
   19    36    > > RETURN                                                   !2
   20    37*     > RETURN                                                   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/s8OLS
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:
163.02 ms | 1403 KiB | 28 Q