3v4l.org

run code in 300+ PHP versions simultaneously
<?php $tree = [ [ "id" => 1573695284631, "name" => "Cars", "pid" => 0, "children" => [ [ "id" => 1573695292010, "name" => "Audi", "pid" => 1573695284631 ], [ "id" => 1573695305619, "name" => "BMW", "pid" => 1573695284631, "children" => [ [ "id" => 1573695328137, "name" => "3 Series", "pid" => 1573695305619 ], [ "id" => 1573695335102, "name" => "X5", "pid" => 1573695305619 ] ] ] ] ], [ "id" => 1573695348647, "name" => "Motorcycles", "pid" => 0, "children" => [ [ "id" => 1573695355619, "name" => "Ducatti", "pid" => 1573695348647 ] ] ] ]; var_dump(getParentNodes($tree, 1573695335102)); function getParentNodes($haystack, $child_node_id) { foreach ($haystack as $element) { if ($element['id'] === $child_node_id) { // return [$element['id']]; // uncomment if you want to include child itself return []; } else if (array_key_exists('children', $element)) { $parentNodes = getParentNodes($element['children'], $child_node_id); if ($parentNodes !== false) { return [$element['id'], ...$parentNodes]; } } } return false; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/umhv9
function name:  (null)
number of ops:  9
compiled vars:  !0 = $tree
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   47     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL_BY_NAME                                       'getParentNodes'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAL_EX                                              1573695335102
          5        DO_FCALL                                      0  $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                                 
   64     8      > RETURN                                                   1

Function getparentnodes:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 27
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 27
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 26
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 26
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 26
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
filename:       /in/umhv9
function name:  getParentNodes
number of ops:  30
compiled vars:  !0 = $haystack, !1 = $child_node_id, !2 = $element, !3 = $parentNodes
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   50     2      > FE_RESET_R                                       $4      !0, ->27
          3    > > FE_FETCH_R                                               $4, !2, ->27
   51     4    >   FETCH_DIM_R                                      ~5      !2, 'id'
          5        IS_IDENTICAL                                             !1, ~5
          6      > JMPZ                                                     ~6, ->10
   53     7    >   FE_FREE                                                  $4
          8      > RETURN                                                   <array>
   51     9*       JMP                                                      ->26
   54    10    >   ARRAY_KEY_EXISTS                                         'children', !2
         11      > JMPZ                                                     ~7, ->26
   55    12    >   INIT_FCALL_BY_NAME                                       'getParentNodes'
         13        CHECK_FUNC_ARG                                           
         14        FETCH_DIM_FUNC_ARG                               $8      !2, 'children'
         15        SEND_FUNC_ARG                                            $8
         16        SEND_VAR_EX                                              !1
         17        DO_FCALL                                      0  $9      
         18        ASSIGN                                                   !3, $9
   57    19        TYPE_CHECK                                  1018          !3
         20      > JMPZ                                                     ~11, ->26
   58    21    >   FETCH_DIM_R                                      ~12     !2, 'id'
         22        INIT_ARRAY                                       ~13     ~12
         23        ADD_ARRAY_UNPACK                                 ~13     !3
         24        FE_FREE                                                  $4
         25      > RETURN                                                   ~13
   50    26    > > JMP                                                      ->3
         27    >   FE_FREE                                                  $4
   63    28      > RETURN                                                   <false>
   64    29*     > RETURN                                                   null

End of function getparentnodes

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.95 ms | 1010 KiB | 14 Q