3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [ ['one' => 'once', 'two' => ['one' => 'twice', 'two' => 'twice', ['one' => 'thrice']], 'three' => 'once',] ]; $needle = 'one'; // the key we're looking for $result = pathFinder($arr, $needle); /** * function pathFinder() returns the path(s) to a key in an array. * * @param array $arr the subject array * @param string $needle the key we're looking for * * @return array the paths leading to the key we're looking for */ function pathFinder(array $arr = [], $needle = ''): array { static $path = ''; static $paths = []; foreach ($arr as $key => $value) { if (is_array($value)) { $path .= $key . "->"; pathFinder($value, $needle); } else { if ($key === $needle) { $paths[] = $path . $key; // store path } } } return $paths; // return all found paths to key $needle } echo '<pre>'; print_r($result); echo '</pre>';
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QOpXI
function name:  (null)
number of ops:  13
compiled vars:  !0 = $arr, !1 = $needle, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
    9     1        ASSIGN                                                   !1, 'one'
   10     2        INIT_FCALL_BY_NAME                                       'pathFinder'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0  $5      
          6        ASSIGN                                                   !2, $5
   38     7        ECHO                                                     '%3Cpre%3E'
   39     8        INIT_FCALL                                               'print_r'
          9        SEND_VAR                                                 !2
         10        DO_ICALL                                                 
   40    11        ECHO                                                     '%3C%2Fpre%3E'
         12      > RETURN                                                   1

Function pathfinder:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 22
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 22
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 16
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 21
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 21
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
filename:       /in/QOpXI
function name:  pathFinder
number of ops:  27
compiled vars:  !0 = $arr, !1 = $needle, !2 = $path, !3 = $paths, !4 = $value, !5 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV_INIT                                        !0      <array>
          1        RECV_INIT                                        !1      ''
   22     2        BIND_STATIC                                              !2
   23     3        BIND_STATIC                                              !3
   25     4      > FE_RESET_R                                       $6      !0, ->22
          5    > > FE_FETCH_R                                       ~7      $6, !4, ->22
          6    >   ASSIGN                                                   !5, ~7
   26     7        TYPE_CHECK                                  128          !4
          8      > JMPZ                                                     ~9, ->16
   27     9    >   CONCAT                                           ~10     !5, '-%3E'
         10        ASSIGN_OP                                     8          !2, ~10
   28    11        INIT_FCALL_BY_NAME                                       'pathFinder'
         12        SEND_VAR_EX                                              !4
         13        SEND_VAR_EX                                              !1
         14        DO_FCALL                                      0          
   26    15      > JMP                                                      ->21
   30    16    >   IS_IDENTICAL                                             !5, !1
         17      > JMPZ                                                     ~13, ->21
   31    18    >   CONCAT                                           ~15     !2, !5
         19        ASSIGN_DIM                                               !3
         20        OP_DATA                                                  ~15
   25    21    > > JMP                                                      ->5
         22    >   FE_FREE                                                  $6
   35    23        VERIFY_RETURN_TYPE                                       !3
         24      > RETURN                                                   !3
   36    25*       VERIFY_RETURN_TYPE                                       
         26*     > RETURN                                                   null

End of function pathfinder

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
137.85 ms | 1003 KiB | 14 Q