3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); final class PathAsKeyDecorator implements \Iterator { private RecursiveIteratorIterator $inner; public function __construct(RecursiveIteratorIterator $inner) { $this->inner = $inner; } public function current() { return $this->inner->current(); } public function next(): void { $this->inner->next(); } public function key() { $path = []; for ($i = 0, $depth = $this->inner->getDepth(); $i <= $depth; $i++) { $path[] = $this->inner->getSubIterator($i)->key(); } return $path; } public function valid(): bool { return $this->inner->valid(); } public function rewind(): void { $this->inner->rewind(); } } $input = [ 'steve' => [ 'id' => [ '#text' => 1, ], ], 'albert' => [ 'id' => [ '#text' => 2, ], ], 'john' => [ 'profil' => [ 'id' => [ '#text' => 3, ], ], ], ]; // this is the filter function that should be customized given your requirements // or create a factory function which produces these types of filter functions $filter = static function ($current, array $path): bool { // with help from the PathAsKeyDecorator // we can decide on the path to the current value return ['id', '#text'] === array_slice($path, -2) // and the current value && 2 === $current; }; // configure the iterator $it = new CallbackFilterIterator( new PathAsKeyDecorator(new RecursiveIteratorIterator(new RecursiveArrayIterator($input))), $filter, ); // traverse the iterator foreach ($it as $path => $val) { print_r([ 'path' => $path, 'val' => $val ]); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 19, Position 2 = 27
Branch analysis from position: 19
2 jumps found. (Code = 78) Position 1 = 20, Position 2 = 27
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
filename:       /in/fmrAk
function name:  (null)
number of ops:  29
compiled vars:  !0 = $input, !1 = $filter, !2 = $it, !3 = $val, !4 = $path
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   DECLARE_CLASS                                            'pathaskeydecorator'
   44     1        ASSIGN                                                   !0, <array>
   66     2        DECLARE_LAMBDA_FUNCTION                          ~6      [0]
          3        ASSIGN                                                   !1, ~6
   76     4        NEW                                              $8      'CallbackFilterIterator'
   77     5        NEW                                              $9      'PathAsKeyDecorator'
          6        NEW                                              $10     'RecursiveIteratorIterator'
          7        NEW                                              $11     'RecursiveArrayIterator'
          8        SEND_VAR_EX                                              !0
          9        DO_FCALL                                      0          
         10        SEND_VAR_NO_REF_EX                                       $11
         11        DO_FCALL                                      0          
         12        SEND_VAR_NO_REF_EX                                       $10
         13        DO_FCALL                                      0          
         14        SEND_VAR_NO_REF_EX                                       $9
         15        SEND_VAR_EX                                              !1
   76    16        DO_FCALL                                      0          
         17        ASSIGN                                                   !2, $8
   82    18      > FE_RESET_R                                       $17     !2, ->27
         19    > > FE_FETCH_R                                       ~18     $17, !3, ->27
         20    >   ASSIGN                                                   !4, ~18
   83    21        INIT_FCALL                                               'print_r'
   84    22        INIT_ARRAY                                       ~20     !4, 'path'
   85    23        ADD_ARRAY_ELEMENT                                ~20     !3, 'val'
         24        SEND_VAL                                                 ~20
   83    25        DO_ICALL                                                 
   82    26      > JMP                                                      ->19
         27    >   FE_FREE                                                  $17
   87    28      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/fmrAk
function name:  {closure}
number of ops:  14
compiled vars:  !0 = $current, !1 = $path
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   69     2        INIT_FCALL                                               'array_slice'
          3        SEND_VAR                                                 !1
          4        SEND_VAL                                                 -2
          5        DO_ICALL                                         $2      
          6        IS_IDENTICAL                                     ~3      $2, <array>
          7      > JMPZ_EX                                          ~3      ~3, ->10
   71     8    >   IS_IDENTICAL                                     ~4      !0, 2
          9        BOOL                                             ~3      ~4
         10    >   VERIFY_RETURN_TYPE                                       ~3
         11      > RETURN                                                   ~3
   72    12*       VERIFY_RETURN_TYPE                                       
         13*     > RETURN                                                   null

End of Dynamic Function 0

Class PathAsKeyDecorator:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fmrAk
function name:  __construct
number of ops:  4
compiled vars:  !0 = $inner
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
   10     1        ASSIGN_OBJ                                               'inner'
          2        OP_DATA                                                  !0
   11     3      > RETURN                                                   null

End of function __construct

Function current:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fmrAk
function name:  current
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   FETCH_OBJ_R                                      ~0      'inner'
          1        INIT_METHOD_CALL                                         ~0, 'current'
          2        DO_FCALL                                      0  $1      
          3      > RETURN                                                   $1
   16     4*     > RETURN                                                   null

End of function current

Function next:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fmrAk
function name:  next
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   FETCH_OBJ_R                                      ~0      'inner'
          1        INIT_METHOD_CALL                                         ~0, 'next'
          2        DO_FCALL                                      0          
   21     3      > RETURN                                                   null

End of function next

Function key:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 7
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 7
Branch analysis from position: 18
Branch analysis from position: 7
filename:       /in/fmrAk
function name:  key
number of ops:  20
compiled vars:  !0 = $path, !1 = $i, !2 = $depth
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   ASSIGN                                                   !0, <array>
   26     1        ASSIGN                                                   !1, 0
          2        FETCH_OBJ_R                                      ~5      'inner'
          3        INIT_METHOD_CALL                                         ~5, 'getDepth'
          4        DO_FCALL                                      0  $6      
          5        ASSIGN                                                   !2, $6
          6      > JMP                                                      ->16
   27     7    >   FETCH_OBJ_R                                      ~9      'inner'
          8        INIT_METHOD_CALL                                         ~9, 'getSubIterator'
          9        SEND_VAR_EX                                              !1
         10        DO_FCALL                                      0  $10     
         11        INIT_METHOD_CALL                                         $10, 'key'
         12        DO_FCALL                                      0  $11     
         13        ASSIGN_DIM                                               !0
         14        OP_DATA                                                  $11
   26    15        PRE_INC                                                  !1
         16    >   IS_SMALLER_OR_EQUAL                                      !1, !2
         17      > JMPNZ                                                    ~13, ->7
   30    18    > > RETURN                                                   !0
   31    19*     > RETURN                                                   null

End of function key

Function valid:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fmrAk
function name:  valid
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   FETCH_OBJ_R                                      ~0      'inner'
          1        INIT_METHOD_CALL                                         ~0, 'valid'
          2        DO_FCALL                                      0  $1      
          3        VERIFY_RETURN_TYPE                                       $1
          4      > RETURN                                                   $1
   36     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function valid

Function rewind:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fmrAk
function name:  rewind
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   FETCH_OBJ_R                                      ~0      'inner'
          1        INIT_METHOD_CALL                                         ~0, 'rewind'
          2        DO_FCALL                                      0          
   41     3      > RETURN                                                   null

End of function rewind

End of class PathAsKeyDecorator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.99 ms | 1008 KiB | 15 Q