3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ "# Grandparent #1", "# Grandparent #2", "## Parent #2-1", "### Child #2-1-1", "## Parent #2-2", "### Child #2-2-1", "### Child #2-2-2", "## Parent #2-3", "## Parent #2-4", "# Grandparent #3", "## Parent #3-1", ]; function recurse(array $lines, int $level = 1): array { $tree = []; $directDescendants = []; foreach($lines as $line) { [$hashes, $name] = explode(' ', $line, 2); $depth = strlen($hashes); if ($depth === $level) { if ($directDescendants) { $parent['children'] = recurse($directDescendants, $level + 1); } $directDescendants = []; // reset collection of direct descendants unset($parent); // destroy reference $parent = ['name' => $name]; // name the member $tree[] = &$parent; // push the reference variable into the tree } elseif ($depth > $level) { $directDescendants[] = $line; // collect only direct descendants of current level parent member } } if ($directDescendants) { $parent['children'] = recurse($directDescendants, $level + 1); } return $tree; } var_export(recurse($array));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/J9RMQ
function name:  (null)
number of ops:  8
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
   41     1        INIT_FCALL                                               'var_export'
          2        INIT_FCALL                                               'recurse'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function recurse:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 40
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 40
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 35
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 28
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 28
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 39
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 39
Branch analysis from position: 40
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 49
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 49
Branch analysis from position: 40
filename:       /in/J9RMQ
function name:  recurse
number of ops:  53
compiled vars:  !0 = $lines, !1 = $level, !2 = $tree, !3 = $directDescendants, !4 = $line, !5 = $hashes, !6 = $name, !7 = $depth, !8 = $parent
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      1
   18     2        ASSIGN                                                   !2, <array>
   19     3        ASSIGN                                                   !3, <array>
   20     4      > FE_RESET_R                                       $11     !0, ->40
          5    > > FE_FETCH_R                                               $11, !4, ->40
   21     6    >   INIT_FCALL                                               'explode'
          7        SEND_VAL                                                 '+'
          8        SEND_VAR                                                 !4
          9        SEND_VAL                                                 2
         10        DO_ICALL                                         $12     
         11        FETCH_LIST_R                                     $13     $12, 0
         12        ASSIGN                                                   !5, $13
         13        FETCH_LIST_R                                     $15     $12, 1
         14        ASSIGN                                                   !6, $15
         15        FREE                                                     $12
   22    16        STRLEN                                           ~17     !5
         17        ASSIGN                                                   !7, ~17
   23    18        IS_IDENTICAL                                             !7, !1
         19      > JMPZ                                                     ~19, ->35
   24    20    > > JMPZ                                                     !3, ->28
   25    21    >   INIT_FCALL_BY_NAME                                       'recurse'
         22        SEND_VAR_EX                                              !3
         23        ADD                                              ~21     !1, 1
         24        SEND_VAL_EX                                              ~21
         25        DO_FCALL                                      0  $22     
         26        ASSIGN_DIM                                               !8, 'children'
         27        OP_DATA                                                  $22
   27    28    >   ASSIGN                                                   !3, <array>
   28    29        UNSET_CV                                                 !8
   29    30        INIT_ARRAY                                       ~24     !6, 'name'
         31        ASSIGN                                                   !8, ~24
   30    32        FETCH_DIM_W                                      $26     !2
         33        ASSIGN_REF                                               $26, !8
   23    34      > JMP                                                      ->39
   31    35    >   IS_SMALLER                                               !1, !7
         36      > JMPZ                                                     ~28, ->39
   32    37    >   ASSIGN_DIM                                               !3
         38        OP_DATA                                                  !4
   20    39    > > JMP                                                      ->5
         40    >   FE_FREE                                                  $11
   35    41      > JMPZ                                                     !3, ->49
   36    42    >   INIT_FCALL_BY_NAME                                       'recurse'
         43        SEND_VAR_EX                                              !3
         44        ADD                                              ~31     !1, 1
         45        SEND_VAL_EX                                              ~31
         46        DO_FCALL                                      0  $32     
         47        ASSIGN_DIM                                               !8, 'children'
         48        OP_DATA                                                  $32
   38    49    >   VERIFY_RETURN_TYPE                                       !2
         50      > RETURN                                                   !2
   39    51*       VERIFY_RETURN_TYPE                                       
         52*     > RETURN                                                   null

End of function recurse

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
269.73 ms | 1019 KiB | 16 Q