3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ [ 'id' => 67941, 'parent_id' => 0, 'name' => 'Requirement', 'children' => [ [ 'id' => 67942, 'parent_id' => 67941, 'name' => 'Sub Requirement', 'children' => [ [ 'id' => 67943, 'parent_id' => 67942, 'name' => 'Sub Sub Requirement', ], ], ], ], ], [ 'id' => 67977, 'parent_id' => 0, 'name' => 'Requirement', 'children' => [ [ 'id' => 67978, 'parent_id' => 67977, 'name' => 'Sub Requirement', 'children' => [ [ 'id' => 67979, 'parent_id' => 67978, 'name' => 'Sub Sub Requirement', 'children' => [ [ 'id' => 67980, 'parent_id' => 67979, 'name' => 'Sub Sub Sub Requirement', ], ], ], ], ], ], ], ]; $needle = 67943; $ritit = new RecursiveIteratorIterator(new RecursiveArrayIterator($array)); $result = null; foreach ($ritit as $leafValue) { $path = []; foreach (range(0, $ritit->getDepth()) as $depth) { $obj = $ritit->getSubIterator($depth); $key = $obj->key(); if ($key !== 'children') { $path[] = $key; } if ($obj->current() === $needle) { $result = $path; break 2; } } } var_export($result);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 42
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 42
Branch analysis from position: 12
2 jumps found. (Code = 77) Position 1 = 20, Position 2 = 40
Branch analysis from position: 20
2 jumps found. (Code = 78) Position 1 = 21, Position 2 = 40
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 32
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 39
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
Branch analysis from position: 32
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 40
Branch analysis from position: 42
Branch analysis from position: 42
filename:       /in/PPXul
function name:  (null)
number of ops:  47
compiled vars:  !0 = $array, !1 = $needle, !2 = $ritit, !3 = $result, !4 = $leafValue, !5 = $path, !6 = $depth, !7 = $obj, !8 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   50     1        ASSIGN                                                   !1, 67943
   52     2        NEW                                              $11     'RecursiveIteratorIterator'
          3        NEW                                              $12     'RecursiveArrayIterator'
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0          
          6        SEND_VAR_NO_REF_EX                                       $12
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !2, $11
   53     9        ASSIGN                                                   !3, null
   54    10      > FE_RESET_R                                       $17     !2, ->42
         11    > > FE_FETCH_R                                               $17, !4, ->42
   55    12    >   ASSIGN                                                   !5, <array>
   56    13        INIT_FCALL                                               'range'
         14        SEND_VAL                                                 0
         15        INIT_METHOD_CALL                                         !2, 'getDepth'
         16        DO_FCALL                                      0  $19     
         17        SEND_VAR                                                 $19
         18        DO_ICALL                                         $20     
         19      > FE_RESET_R                                       $21     $20, ->40
         20    > > FE_FETCH_R                                               $21, !6, ->40
   57    21    >   INIT_METHOD_CALL                                         !2, 'getSubIterator'
         22        SEND_VAR_EX                                              !6
         23        DO_FCALL                                      0  $22     
         24        ASSIGN                                                   !7, $22
   58    25        INIT_METHOD_CALL                                         !7, 'key'
         26        DO_FCALL                                      0  $24     
         27        ASSIGN                                                   !8, $24
   59    28        IS_NOT_IDENTICAL                                         !8, 'children'
         29      > JMPZ                                                     ~26, ->32
   60    30    >   ASSIGN_DIM                                               !5
         31        OP_DATA                                                  !8
   62    32    >   INIT_METHOD_CALL                                         !7, 'current'
         33        DO_FCALL                                      0  $28     
         34        IS_IDENTICAL                                             !1, $28
         35      > JMPZ                                                     ~29, ->39
   63    36    >   ASSIGN                                                   !3, !5
   64    37        FE_FREE                                                  $21
         38      > JMP                                                      ->42
   56    39    > > JMP                                                      ->20
         40    >   FE_FREE                                                  $21
   54    41      > JMP                                                      ->11
         42    >   FE_FREE                                                  $17
   69    43        INIT_FCALL                                               'var_export'
         44        SEND_VAR                                                 !3
         45        DO_ICALL                                                 
         46      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
217.45 ms | 1016 KiB | 15 Q