3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getTree($treeByParent, $treeByCat, $id, $lastId) { $gTree = []; if ($id != $lastId) { $gTree[] = $id; if ( isset($treeByCat[ $id ]) && count($treeByCat[ $id ]) > 0 ) $gTree = array_merge( getTree($treeByParent, $treeByCat, $treeByCat[ $id ], $id), $gTree); if ( isset($treeByParent[ $id ]) && count($treeByParent[ $id ]) > 0 ) { foreach ($treeByParent[ $id ] as $cat) { $gTree = array_merge( getTree($treeByParent, $treeByCat, $cat, $id), $gTree); } } return $gTree; } } function display_parent_nodes($data, $id) { $tree = array( "parents" => array(), "children" => array(), "full" => array() ); $current = $data[$id]; $parent_id = $current === NULL ? "NULL" : $current; $childIds = array($id); $tree['parents'][] = $parent_id; while (isset($data[$parent_id])) { $current = $data[$parent_id]; $parent_id = $current === NULL ? "NULL" : $current; $tree['parents'][] = $parent_id; } echo implode(" > ", array_reverse($tree['parents'])); while ( count($childIds) > 0 ) { print_r($childIds); foreach($childIds as $child) { $children = array_keys($data, $child); foreach ($children as $ch) { $current = $data[$ch]; $child_id = $current === NULL ? "NULL" : $current; $tree['children'][] = $child_id; } $childIds = $children; } } echo "\n". implode(" > ", $tree['children']); } $treeByParent = array( 0 => array(1, 2), 1 => array(3, 7), 3 => array(4), 4 => array(5), 5 => array(6, 8) ); $treeByCat = array( 1 => 0, 2 => 0, 3 => 1, 7 => 1, 4 => 3, 5 => 4, 6 => 5, 8 => 5 ); print_r( display_parent_nodes($treeByCat, 4) ); //print_r( getTree($treeByParent, $treeByCat, 4, 0) );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G8u7p
function name:  (null)
number of ops:  10
compiled vars:  !0 = $treeByParent, !1 = $treeByCat
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   ASSIGN                                                   !0, <array>
   83     1        ASSIGN                                                   !1, <array>
   96     2        INIT_FCALL                                               'print_r'
          3        INIT_FCALL                                               'display_parent_nodes'
          4        SEND_VAR                                                 !1
          5        SEND_VAL                                                 4
          6        DO_FCALL                                      0  $4      
          7        SEND_VAR                                                 $4
          8        DO_ICALL                                                 
   98     9      > RETURN                                                   1

Function gettree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 53
Branch analysis from position: 7
2 jumps found. (Code = 46) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 29
Branch analysis from position: 16
2 jumps found. (Code = 46) Position 1 = 31, Position 2 = 35
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 52
Branch analysis from position: 36
2 jumps found. (Code = 77) Position 1 = 38, Position 2 = 51
Branch analysis from position: 38
2 jumps found. (Code = 78) Position 1 = 39, Position 2 = 51
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 51
Branch analysis from position: 52
Branch analysis from position: 35
Branch analysis from position: 29
Branch analysis from position: 15
Branch analysis from position: 53
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G8u7p
function name:  getTree
number of ops:  54
compiled vars:  !0 = $treeByParent, !1 = $treeByCat, !2 = $id, !3 = $lastId, !4 = $gTree, !5 = $cat
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
    6     4        ASSIGN                                                   !4, <array>
    8     5        IS_NOT_EQUAL                                             !2, !3
          6      > JMPZ                                                     ~7, ->53
    9     7    >   ASSIGN_DIM                                               !4
          8        OP_DATA                                                  !2
   11     9        ISSET_ISEMPTY_DIM_OBJ                         0  ~9      !1, !2
         10      > JMPZ_EX                                          ~9      ~9, ->15
         11    >   FETCH_DIM_R                                      ~10     !1, !2
         12        COUNT                                            ~11     ~10
         13        IS_SMALLER                                       ~12     0, ~11
         14        BOOL                                             ~9      ~12
         15    > > JMPZ                                                     ~9, ->29
         16    >   INIT_FCALL                                               'array_merge'
         17        INIT_FCALL_BY_NAME                                       'getTree'
         18        SEND_VAR_EX                                              !0
         19        SEND_VAR_EX                                              !1
         20        CHECK_FUNC_ARG                                           
         21        FETCH_DIM_FUNC_ARG                               $13     !1, !2
         22        SEND_FUNC_ARG                                            $13
         23        SEND_VAR_EX                                              !2
         24        DO_FCALL                                      0  $14     
         25        SEND_VAR                                                 $14
         26        SEND_VAR                                                 !4
         27        DO_ICALL                                         $15     
         28        ASSIGN                                                   !4, $15
   13    29    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~17     !0, !2
         30      > JMPZ_EX                                          ~17     ~17, ->35
         31    >   FETCH_DIM_R                                      ~18     !0, !2
         32        COUNT                                            ~19     ~18
         33        IS_SMALLER                                       ~20     0, ~19
         34        BOOL                                             ~17     ~20
         35    > > JMPZ                                                     ~17, ->52
   15    36    >   FETCH_DIM_R                                      ~21     !0, !2
         37      > FE_RESET_R                                       $22     ~21, ->51
         38    > > FE_FETCH_R                                               $22, !5, ->51
   16    39    >   INIT_FCALL                                               'array_merge'
         40        INIT_FCALL_BY_NAME                                       'getTree'
         41        SEND_VAR_EX                                              !0
         42        SEND_VAR_EX                                              !1
         43        SEND_VAR_EX                                              !5
         44        SEND_VAR_EX                                              !2
         45        DO_FCALL                                      0  $23     
         46        SEND_VAR                                                 $23
         47        SEND_VAR                                                 !4
         48        DO_ICALL                                         $24     
         49        ASSIGN                                                   !4, $24
   15    50      > JMP                                                      ->38
         51    >   FE_FREE                                                  $22
   21    52    > > RETURN                                                   !4
   23    53    > > RETURN                                                   null

End of function gettree

Function display_parent_nodes:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 17
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 68
Branch analysis from position: 68
2 jumps found. (Code = 44) Position 1 = 71, Position 2 = 40
Branch analysis from position: 71
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
2 jumps found. (Code = 77) Position 1 = 44, Position 2 = 67
Branch analysis from position: 44
2 jumps found. (Code = 78) Position 1 = 45, Position 2 = 67
Branch analysis from position: 45
2 jumps found. (Code = 77) Position 1 = 51, Position 2 = 64
Branch analysis from position: 51
2 jumps found. (Code = 78) Position 1 = 52, Position 2 = 64
Branch analysis from position: 52
2 jumps found. (Code = 43) Position 1 = 56, Position 2 = 58
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 51
Branch analysis from position: 51
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 51
Branch analysis from position: 51
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
Branch analysis from position: 64
Branch analysis from position: 67
2 jumps found. (Code = 44) Position 1 = 71, Position 2 = 40
Branch analysis from position: 71
Branch analysis from position: 40
Branch analysis from position: 67
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 23
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 17
Branch analysis from position: 30
Branch analysis from position: 17
Branch analysis from position: 23
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 17
Branch analysis from position: 30
Branch analysis from position: 17
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
filename:       /in/G8u7p
function name:  display_parent_nodes
number of ops:  79
compiled vars:  !0 = $data, !1 = $id, !2 = $tree, !3 = $current, !4 = $parent_id, !5 = $childIds, !6 = $child, !7 = $children, !8 = $ch, !9 = $child_id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   28     2        ASSIGN                                                   !2, <array>
   34     3        FETCH_DIM_R                                      ~11     !0, !1
          4        ASSIGN                                                   !3, ~11
   35     5        TYPE_CHECK                                    2          !3
          6      > JMPZ                                                     ~13, ->9
          7    >   QM_ASSIGN                                        ~14     'NULL'
          8      > JMP                                                      ->10
          9    >   QM_ASSIGN                                        ~14     !3
         10    >   ASSIGN                                                   !4, ~14
   37    11        INIT_ARRAY                                       ~16     !1
         12        ASSIGN                                                   !5, ~16
   39    13        FETCH_DIM_W                                      $18     !2, 'parents'
         14        ASSIGN_DIM                                               $18
         15        OP_DATA                                                  !4
   41    16      > JMP                                                      ->28
   42    17    >   FETCH_DIM_R                                      ~20     !0, !4
         18        ASSIGN                                                   !3, ~20
   43    19        TYPE_CHECK                                    2          !3
         20      > JMPZ                                                     ~22, ->23
         21    >   QM_ASSIGN                                        ~23     'NULL'
         22      > JMP                                                      ->24
         23    >   QM_ASSIGN                                        ~23     !3
         24    >   ASSIGN                                                   !4, ~23
   44    25        FETCH_DIM_W                                      $25     !2, 'parents'
         26        ASSIGN_DIM                                               $25
         27        OP_DATA                                                  !4
   41    28    >   ISSET_ISEMPTY_DIM_OBJ                         0          !0, !4
         29      > JMPNZ                                                    ~27, ->17
   47    30    >   INIT_FCALL                                               'implode'
         31        SEND_VAL                                                 '+%3E+'
         32        INIT_FCALL                                               'array_reverse'
         33        FETCH_DIM_R                                      ~28     !2, 'parents'
         34        SEND_VAL                                                 ~28
         35        DO_ICALL                                         $29     
         36        SEND_VAR                                                 $29
         37        DO_ICALL                                         $30     
         38        ECHO                                                     $30
   49    39      > JMP                                                      ->68
   51    40    >   INIT_FCALL                                               'print_r'
         41        SEND_VAR                                                 !5
         42        DO_ICALL                                                 
   53    43      > FE_RESET_R                                       $32     !5, ->67
         44    > > FE_FETCH_R                                               $32, !6, ->67
   55    45    >   INIT_FCALL                                               'array_keys'
         46        SEND_VAR                                                 !0
         47        SEND_VAR                                                 !6
         48        DO_ICALL                                         $33     
         49        ASSIGN                                                   !7, $33
   57    50      > FE_RESET_R                                       $35     !7, ->64
         51    > > FE_FETCH_R                                               $35, !8, ->64
   59    52    >   FETCH_DIM_R                                      ~36     !0, !8
         53        ASSIGN                                                   !3, ~36
   60    54        TYPE_CHECK                                    2          !3
         55      > JMPZ                                                     ~38, ->58
         56    >   QM_ASSIGN                                        ~39     'NULL'
         57      > JMP                                                      ->59
         58    >   QM_ASSIGN                                        ~39     !3
         59    >   ASSIGN                                                   !9, ~39
   61    60        FETCH_DIM_W                                      $41     !2, 'children'
         61        ASSIGN_DIM                                               $41
         62        OP_DATA                                                  !9
   57    63      > JMP                                                      ->51
         64    >   FE_FREE                                                  $35
   64    65        ASSIGN                                                   !5, !7
   53    66      > JMP                                                      ->44
         67    >   FE_FREE                                                  $32
   49    68    >   COUNT                                            ~44     !5
         69        IS_SMALLER                                               0, ~44
         70      > JMPNZ                                                    ~45, ->40
   68    71    >   INIT_FCALL                                               'implode'
         72        SEND_VAL                                                 '+%3E+'
         73        FETCH_DIM_R                                      ~46     !2, 'children'
         74        SEND_VAL                                                 ~46
         75        DO_ICALL                                         $47     
         76        CONCAT                                           ~48     '%0A', $47
         77        ECHO                                                     ~48
   70    78      > RETURN                                                   null

End of function display_parent_nodes

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
166.43 ms | 1406 KiB | 24 Q