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($tree, $id) { $current = $tree[$id]; $parent_id = $current === NULL ? "NULL" : $current; $parents = array(); $parents[] = $parent_id; while (isset($tree[$parent_id])) { $current = $tree[$parent_id]; $parent_id = $current === NULL ? "NULL" : $current; $parents[] = $parent_id; } echo implode(" > ", array_reverse($parents)); } $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/meQUS
function name:  (null)
number of ops:  10
compiled vars:  !0 = $treeByParent, !1 = $treeByCat
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   ASSIGN                                                   !0, <array>
   53     1        ASSIGN                                                   !1, <array>
   66     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                                                 
   68     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/meQUS
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 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 26, Position 2 = 14
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 20
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 26, Position 2 = 14
Branch analysis from position: 26
Branch analysis from position: 14
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 26, Position 2 = 14
Branch analysis from position: 26
Branch analysis from position: 14
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
filename:       /in/meQUS
function name:  display_parent_nodes
number of ops:  35
compiled vars:  !0 = $tree, !1 = $id, !2 = $current, !3 = $parent_id, !4 = $parents
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   28     2        FETCH_DIM_R                                      ~5      !0, !1
          3        ASSIGN                                                   !2, ~5
   29     4        TYPE_CHECK                                    2          !2
          5      > JMPZ                                                     ~7, ->8
          6    >   QM_ASSIGN                                        ~8      'NULL'
          7      > JMP                                                      ->9
          8    >   QM_ASSIGN                                        ~8      !2
          9    >   ASSIGN                                                   !3, ~8
   30    10        ASSIGN                                                   !4, <array>
   31    11        ASSIGN_DIM                                               !4
         12        OP_DATA                                                  !3
   33    13      > JMP                                                      ->24
   34    14    >   FETCH_DIM_R                                      ~12     !0, !3
         15        ASSIGN                                                   !2, ~12
   35    16        TYPE_CHECK                                    2          !2
         17      > JMPZ                                                     ~14, ->20
         18    >   QM_ASSIGN                                        ~15     'NULL'
         19      > JMP                                                      ->21
         20    >   QM_ASSIGN                                        ~15     !2
         21    >   ASSIGN                                                   !3, ~15
   36    22        ASSIGN_DIM                                               !4
         23        OP_DATA                                                  !3
   33    24    >   ISSET_ISEMPTY_DIM_OBJ                         0          !0, !3
         25      > JMPNZ                                                    ~18, ->14
   39    26    >   INIT_FCALL                                               'implode'
         27        SEND_VAL                                                 '+%3E+'
         28        INIT_FCALL                                               'array_reverse'
         29        SEND_VAR                                                 !4
         30        DO_ICALL                                         $19     
         31        SEND_VAR                                                 $19
         32        DO_ICALL                                         $20     
         33        ECHO                                                     $20
   40    34      > RETURN                                                   null

End of function display_parent_nodes

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153 ms | 1407 KiB | 22 Q