3v4l.org

run code in 500+ PHP versions simultaneously
<?php $arr = [['ID' => 1, 'Name' => 'My Name', 'GroupLocations' => [['GroupId' => 111, 'LocationId' => 222, 'Location' => ['Name' => 'MyLocation', 'Street1' => '555 Somewhere Lane']]]]]; $results = []; // the container for the search result(s) $key = 'Name'; // the key we are looking for findKey($arr, $key, $results); // invoke the search function /* NEW */ $count = count($results); for($i = 0; $i < $count; $i++) { $myVar = 'value' . $i; // set your base variable name $$myVar = $results[$i][$key]; // assign found value to new variable echo 'created variable: ' . '$value' . $i . ' - value : ' . $$myVar . "\n"; } echo $value0 ? $value0 : 'n/a'; echo $value1 ? $value1 : 'n/a'; // etc. /* END NEW */ /** * @param array $arr the multidimensional array we are searching * @param string $key the key we are looking for * @param $results passed by reference - in case the key is found, this array 'stores' the corresponding key-value pair. */ function findKey($arr = [], $key = '', &$results = []) { foreach ($arr as $key0 => $value0) { if ($key0 == $key && !is_array($value0)) { $results[][$key] = $value0; } if (is_array($value0)) { findKey($value0, $key, $results); } } return false; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 12
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 30
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 35
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 35
Branch analysis from position: 33
Branch analysis from position: 35
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 12
Branch analysis from position: 27
Branch analysis from position: 12
filename:       /in/NSgKQ
function name:  (null)
number of ops:  38
compiled vars:  !0 = $arr, !1 = $results, !2 = $key, !3 = $count, !4 = $i, !5 = $myVar, !6 = $value0, !7 = $value1
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
    5     1        ASSIGN                                                       !1, <array>
    6     2        ASSIGN                                                       !2, 'Name'
    7     3        INIT_FCALL_BY_NAME                                           'findKey'
          4        SEND_VAR_EX                                                  !0
          5        SEND_VAR_EX                                                  !2
          6        SEND_VAR_EX                                                  !1
          7        DO_FCALL                                          0          
   10     8        COUNT                                                ~12     !1
          9        ASSIGN                                                       !3, ~12
   11    10        ASSIGN                                                       !4, 0
         11      > JMP                                                          ->25
   12    12    >   CONCAT                                               ~15     'value', !4
         13        ASSIGN                                                       !5, ~15
   13    14        FETCH_DIM_R                                          ~18     !1, !4
         15        FETCH_DIM_R                                          ~19     ~18, !2
         16        FETCH_W                          local               $17     !5
         17        ASSIGN                                                       $17, ~19
   14    18        CONCAT                                               ~21     'created+variable%3A+%24value', !4
         19        CONCAT                                               ~22     ~21, '+-+value+%3A+'
         20        FETCH_R                          local               ~23     !5
         21        CONCAT                                               ~24     ~22, ~23
         22        CONCAT                                               ~25     ~24, '%0A'
         23        ECHO                                                         ~25
   11    24        PRE_INC                                                      !4
         25    >   IS_SMALLER                                                   !4, !3
         26      > JMPNZ                                                        ~27, ->12
   17    27    > > JMPZ                                                         !6, ->30
         28    >   QM_ASSIGN                                            ~28     !6
         29      > JMP                                                          ->31
         30    >   QM_ASSIGN                                            ~28     'n%2Fa'
         31    >   ECHO                                                         ~28
   18    32      > JMPZ                                                         !7, ->35
         33    >   QM_ASSIGN                                            ~29     !7
         34      > JMP                                                          ->36
         35    >   QM_ASSIGN                                            ~29     'n%2Fa'
         36    >   ECHO                                                         ~29
   39    37      > RETURN                                                       1

Function findkey:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 23
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 23
Branch analysis from position: 5
2 jumps found. (Code = 46) Position 1 = 8, Position 2 = 11
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 15
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 22
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 22
Branch analysis from position: 15
Branch analysis from position: 11
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/NSgKQ
function name:  findKey
number of ops:  26
compiled vars:  !0 = $arr, !1 = $key, !2 = $results, !3 = $value0, !4 = $key0
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   28     0  E >   RECV_INIT                                            !0      <array>
          1        RECV_INIT                                            !1      ''
          2        RECV_INIT                                            !2      <array>
   30     3      > FE_RESET_R                                           $5      !0, ->23
          4    > > FE_FETCH_R                                           ~6      $5, !3, ->23
          5    >   ASSIGN                                                       !4, ~6
   31     6        IS_EQUAL                                             ~8      !4, !1
          7      > JMPZ_EX                                              ~8      ~8, ->11
          8    >   TYPE_CHECK                                      128  ~9      !3
          9        BOOL_NOT                                             ~10     ~9
         10        BOOL                                                 ~8      ~10
         11    > > JMPZ                                                         ~8, ->15
   32    12    >   FETCH_DIM_W                                          $11     !2
         13        ASSIGN_DIM                                                   $11, !1
         14        OP_DATA                                                      !3
   34    15    >   TYPE_CHECK                                      128          !3
         16      > JMPZ                                                         ~13, ->22
   35    17    >   INIT_FCALL_BY_NAME                                           'findKey'
         18        SEND_VAR_EX                                                  !3
         19        SEND_VAR_EX                                                  !1
         20        SEND_VAR_EX                                                  !2
         21        DO_FCALL                                          0          
   30    22    > > JMP                                                          ->4
         23    >   FE_FREE                                                      $5
   38    24      > RETURN                                                       <false>
   39    25*     > RETURN                                                       null

End of function findkey

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
156.86 ms | 2052 KiB | 13 Q