3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getParentAndChild($data, $id) { $tree = array( 'parents' => array(), 'children' => array(), 'full' => array() ); $current = $data[$id]; $parent_id = $current === NULL ? 'NULL' : $current; $child_ids = array($id); $tree['parents'][] = $parent_id; while ( isset($data[$parent_id]) ) { $current = $data[$parent_id]; $parent_id = $current === NULL ? 'NULL' : $current; if ($parent_id > 0) $tree['parents'][] = $parent_id; } while ( count($child_ids) > 0 ) { foreach($child_ids as $child_id) { $children = array_keys($data, $child_id); foreach ($children as $child) $tree['children'][] = $child; $child_ids = $children; } } $tree['full'] = array_merge(array_reverse($tree['parents']), $tree['children']); return $tree['full']; } $treeByCat = array( 1 => 0, 2 => 0, 3 => 1, 7 => 1, 4 => 3, 5 => 4, 6 => 5, 8 => 5 ); print_r( getParentAndChild($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/pCQNU
function name:  (null)
number of ops:  9
compiled vars:  !0 = $treeByCat
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   ASSIGN                                                   !0, <array>
   61     1        INIT_FCALL                                               'print_r'
          2        INIT_FCALL                                               'getparentandchild'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 4
          5        DO_FCALL                                      0  $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                                 
   63     8      > RETURN                                                   1

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

End of function getparentandchild

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.45 ms | 1398 KiB | 22 Q