3v4l.org

run code in 300+ PHP versions simultaneously
<?php function findMyThing($needle, $haystack) { // We need to set up a stack array + a while loop to avoid recursive functions for those are evil. // Recursive functions would also complicate things further in regard of returning. $stack = [ [ 'prefix' => [], 'value' => $haystack ] ]; // As long as there's still something there, don't stop while(count($stack) > 0) { // Copy the current stack and create a new, empty one $currentStack = $stack; $stack = []; // Work the stack for($i = 0; $i < count($currentStack); $i++) { // Iterate over the actual array foreach($currentStack[$i]['value'] as $key => $value) { // If the value is an array, then // 1. the key is a string (so we need to match against it) // 2. we might have to go deeper if(is_array($value)) { // We need to build the current prefix list regardless of what we're gonna do below $prefix = $currentStack[$i]['prefix']; $prefix[] = $key; // If the current key, is the one we're looking for, heureka! if($key == $needle) { return $prefix; } // Otherwise, push prefix & value onto the stack for the next loop to pick up else { $stack[] = [ 'prefix' => $prefix, 'value' => $value ]; } } // If the value is NOT an array, then // 1. the key is an integer, so we DO NOT want to match against it // 2. we need to match against the value itself elseif($value == $needle) { // This time append $value, not $key $prefix = $currentStack[$i]['prefix']; $prefix[] = $value; return $prefix; } } } } // At this point we searched the entire array and didn't find anything, so we return an empty array return []; } $arr = [ 'USA' => [ 'Alabama' => [ 'Montgomery', 'Birmingham' ], 'Arizona' => [ 'Phoenix', 'Mesa', 'Gilbert' ] ], 'Germany' => [ 'West Germany' => [ 'Bonn', 'Cologne' ] ] ]; var_dump(findMyThing('Alabama', $arr)); var_dump(findMyThing('Cologne', $arr));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/JY90G
function name:  (null)
number of ops:  16
compiled vars:  !0 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   ASSIGN                                                   !0, <array>
   91     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'findmything'
          3        SEND_VAL                                                 'Alabama'
          4        SEND_VAR                                                 !0
          5        DO_FCALL                                      0  $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                                 
   92     8        INIT_FCALL                                               'var_dump'
          9        INIT_FCALL                                               'findmything'
         10        SEND_VAL                                                 'Cologne'
         11        SEND_VAR                                                 !0
         12        DO_FCALL                                      0  $4      
         13        SEND_VAR                                                 $4
         14        DO_ICALL                                                 
         15      > RETURN                                                   1

Function findmything:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
2 jumps found. (Code = 44) Position 1 = 51, Position 2 = 7
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
2 jumps found. (Code = 44) Position 1 = 48, Position 2 = 11
Branch analysis from position: 48
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 43
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 43
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 33
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 28
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 42
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
Branch analysis from position: 43
2 jumps found. (Code = 44) Position 1 = 48, Position 2 = 11
Branch analysis from position: 48
Branch analysis from position: 11
Branch analysis from position: 43
filename:       /in/JY90G
function name:  findMyThing
number of ops:  53
compiled vars:  !0 = $needle, !1 = $haystack, !2 = $stack, !3 = $currentStack, !4 = $i, !5 = $value, !6 = $key, !7 = $prefix
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   10     2        INIT_ARRAY                                       ~8      <array>, 'prefix'
   11     3        ADD_ARRAY_ELEMENT                                ~8      !1, 'value'
          4        INIT_ARRAY                                       ~9      ~8
    7     5        ASSIGN                                                   !2, ~9
   15     6      > JMP                                                      ->48
   18     7    >   ASSIGN                                                   !3, !2
   19     8        ASSIGN                                                   !2, <array>
   21     9        ASSIGN                                                   !4, 0
         10      > JMP                                                      ->45
   24    11    >   FETCH_DIM_R                                      ~14     !3, !4
         12        FETCH_DIM_R                                      ~15     ~14, 'value'
         13      > FE_RESET_R                                       $16     ~15, ->43
         14    > > FE_FETCH_R                                       ~17     $16, !5, ->43
         15    >   ASSIGN                                                   !6, ~17
   29    16        TYPE_CHECK                                  128          !5
         17      > JMPZ                                                     ~19, ->33
   32    18    >   FETCH_DIM_R                                      ~20     !3, !4
         19        FETCH_DIM_R                                      ~21     ~20, 'prefix'
         20        ASSIGN                                                   !7, ~21
   33    21        ASSIGN_DIM                                               !7
         22        OP_DATA                                                  !6
   35    23        IS_EQUAL                                                 !6, !0
         24      > JMPZ                                                     ~24, ->28
   37    25    >   FE_FREE                                                  $16
         26      > RETURN                                                   !7
         27*       JMP                                                      ->32
   44    28    >   INIT_ARRAY                                       ~26     !7, 'prefix'
   45    29        ADD_ARRAY_ELEMENT                                ~26     !5, 'value'
   42    30        ASSIGN_DIM                                               !2
   45    31        OP_DATA                                                  ~26
         32      > JMP                                                      ->42
   52    33    >   IS_EQUAL                                                 !5, !0
         34      > JMPZ                                                     ~27, ->42
   55    35    >   FETCH_DIM_R                                      ~28     !3, !4
         36        FETCH_DIM_R                                      ~29     ~28, 'prefix'
         37        ASSIGN                                                   !7, ~29
   56    38        ASSIGN_DIM                                               !7
         39        OP_DATA                                                  !5
   57    40        FE_FREE                                                  $16
         41      > RETURN                                                   !7
   24    42    > > JMP                                                      ->14
         43    >   FE_FREE                                                  $16
   21    44        PRE_INC                                                  !4
         45    >   COUNT                                            ~33     !3
         46        IS_SMALLER                                               !4, ~33
         47      > JMPNZ                                                    ~34, ->11
   15    48    >   COUNT                                            ~35     !2
         49        IS_SMALLER                                               0, ~35
         50      > JMPNZ                                                    ~36, ->7
   63    51    > > RETURN                                                   <array>
   64    52*     > RETURN                                                   null

End of function findmything

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.75 ms | 1394 KiB | 17 Q