3v4l.org

run code in 300+ PHP versions simultaneously
<?php $tree = [ 0 => [ 'name' => 'root', 'data' => 0, 'children' => [ '1' => [ 'name' => 'Auto', 'data' => 0, 'children' => [ '2' => [ 'name' => 'Tyres', 'data' => 0, 'children' => [], ], '3' => [ 'name' => 'Audio', 'data' => 1, 'children' => [], ], ], ], '4' => [ 'name' => 'Dishes', 'data' => 0, 'children' => [ '5' => [ 'name' => 'Forks', 'data' => 0, 'children' => [], ], '6' => [ 'name' => 'Knives', 'data' => 0, 'children' => [ '7' => [ 'name' => 'Stainless steel', 'data' => 0, 'children' => [], ], '8' => [ 'name' => 'Silver', 'data' => 0, 'children' => [], ], ], ], ], ], ] ] ]; #var_dump($tree); function reduceTree($tree) { foreach ($tree['children'] as $key => $value) { if ($value['children']) { $tree['children'][$key] = reduceTree($value); } if (!$tree['data'] && !$tree['children']) { unset($tree['children'][$key]); } } return $tree; } $new = reduceTree($tree[0]); var_dump($new);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1h7h0
function name:  (null)
number of ops:  10
compiled vars:  !0 = $tree, !1 = $new
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   70     1        INIT_FCALL                                               'reducetree'
          2        FETCH_DIM_R                                      ~3      !0, 0
          3        SEND_VAL                                                 ~3
          4        DO_FCALL                                      0  $4      
          5        ASSIGN                                                   !1, $4
   71     6        INIT_FCALL                                               'var_dump'
          7        SEND_VAR                                                 !1
          8        DO_ICALL                                                 
          9      > RETURN                                                   1

Function reducetree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 23
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 23
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 13
Branch analysis from position: 7
2 jumps found. (Code = 46) Position 1 = 16, Position 2 = 19
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 22
Branch analysis from position: 19
Branch analysis from position: 13
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/1h7h0
function name:  reduceTree
number of ops:  26
compiled vars:  !0 = $tree, !1 = $value, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   RECV                                             !0      
   58     1        FETCH_DIM_R                                      ~3      !0, 'children'
          2      > FE_RESET_R                                       $4      ~3, ->23
          3    > > FE_FETCH_R                                       ~5      $4, !1, ->23
          4    >   ASSIGN                                                   !2, ~5
   59     5        FETCH_DIM_R                                      ~7      !1, 'children'
          6      > JMPZ                                                     ~7, ->13
   60     7    >   INIT_FCALL_BY_NAME                                       'reduceTree'
          8        SEND_VAR_EX                                              !1
          9        DO_FCALL                                      0  $10     
         10        FETCH_DIM_W                                      $8      !0, 'children'
         11        ASSIGN_DIM                                               $8, !2
         12        OP_DATA                                                  $10
   63    13    >   FETCH_DIM_R                                      ~11     !0, 'data'
         14        BOOL_NOT                                         ~12     ~11
         15      > JMPZ_EX                                          ~12     ~12, ->19
         16    >   FETCH_DIM_R                                      ~13     !0, 'children'
         17        BOOL_NOT                                         ~14     ~13
         18        BOOL                                             ~12     ~14
         19    > > JMPZ                                                     ~12, ->22
   64    20    >   FETCH_DIM_UNSET                                  $15     !0, 'children'
         21        UNSET_DIM                                                $15, !2
   58    22    > > JMP                                                      ->3
         23    >   FE_FREE                                                  $4
   67    24      > RETURN                                                   !0
   68    25*     > RETURN                                                   null

End of function reducetree

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.64 ms | 1391 KiB | 16 Q