3v4l.org

run code in 300+ PHP versions simultaneously
<?php function buildTree(array $elements, $parentId = '-1') { $branch = array(); foreach ($elements as $key => $value) { if ($value == $parentId) { $children = buildTree($elements, $key); $branch[$parentId] = $children; } } $branch[$parentId] = []; return $branch; } function recursivePrint($elements) { foreach ($elements as $key => $value) { echo 'count: '.count($value)."\n"; echo $key."\n"; recursivePrint($value); } } $childParent = ['0-1-2-3-4' => '-1', '0-1' => '-1', '3-4' => '0-1', '5-6' => '3-4', '5-6' => '01324']; $tree = buildTree($childParent); recursivePrint($tree);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s7oI0
function name:  (null)
number of ops:  9
compiled vars:  !0 = $childParent, !1 = $tree
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   ASSIGN                                                   !0, <array>
   28     1        INIT_FCALL                                               'buildtree'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $3      
          4        ASSIGN                                                   !1, $3
   29     5        INIT_FCALL                                               'recursiveprint'
          6        SEND_VAR                                                 !1
          7        DO_FCALL                                      0          
          8      > RETURN                                                   1

Function buildtree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 16
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 16
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 15
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 15
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/s7oI0
function name:  buildTree
number of ops:  21
compiled vars:  !0 = $elements, !1 = $parentId, !2 = $branch, !3 = $value, !4 = $key, !5 = $children
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      '-1'
    4     2        ASSIGN                                                   !2, <array>
    6     3      > FE_RESET_R                                       $7      !0, ->16
          4    > > FE_FETCH_R                                       ~8      $7, !3, ->16
          5    >   ASSIGN                                                   !4, ~8
    7     6        IS_EQUAL                                                 !3, !1
          7      > JMPZ                                                     ~10, ->15
    8     8    >   INIT_FCALL_BY_NAME                                       'buildTree'
          9        SEND_VAR_EX                                              !0
         10        SEND_VAR_EX                                              !4
         11        DO_FCALL                                      0  $11     
         12        ASSIGN                                                   !5, $11
    9    13        ASSIGN_DIM                                               !2, !1
         14        OP_DATA                                                  !5
    6    15    > > JMP                                                      ->4
         16    >   FE_FREE                                                  $7
   13    17        ASSIGN_DIM                                               !2, !1
         18        OP_DATA                                                  <array>
   14    19      > RETURN                                                   !2
   15    20*     > RETURN                                                   null

End of function buildtree

Function recursiveprint:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 14
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 14
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/s7oI0
function name:  recursivePrint
number of ops:  16
compiled vars:  !0 = $elements, !1 = $value, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   19     1      > FE_RESET_R                                       $3      !0, ->14
          2    > > FE_FETCH_R                                       ~4      $3, !1, ->14
          3    >   ASSIGN                                                   !2, ~4
   20     4        COUNT                                            ~6      !1
          5        CONCAT                                           ~7      'count%3A+', ~6
          6        CONCAT                                           ~8      ~7, '%0A'
          7        ECHO                                                     ~8
   21     8        CONCAT                                           ~9      !2, '%0A'
          9        ECHO                                                     ~9
   22    10        INIT_FCALL_BY_NAME                                       'recursivePrint'
         11        SEND_VAR_EX                                              !1
         12        DO_FCALL                                      0          
   19    13      > JMP                                                      ->2
         14    >   FE_FREE                                                  $3
   24    15      > RETURN                                                   null

End of function recursiveprint

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
167.15 ms | 1403 KiB | 15 Q