3v4l.org

run code in 500+ PHP versions simultaneously
<?php function getValue($haystack, $indexes) { $indices = explode(',', $indexes); $finalIndex = array_pop($indices); $keys = []; foreach ($indices as $keys[]) { $keys[] = 'children'; } array_push($keys, $finalIndex, 'country', 'city'); //var_export($keys); foreach ($keys as $level => $key) { if (!key_exists($key, $haystack)) { throw new Exception( sprintf( "Path attempt failed for [%s]. No `%s` key found on levelIndex %d", implode('][', $keys), $key, $level ) ); } $haystack = $haystack[$key]; } return $haystack; } $test = [ [ 'children' => [ [ 'children' => [ [ 'country' => [ 'city' => 'Paris', ] ], [ 'country' => [ 'city' => 'Kyiv', ] ] ] ] ] ], [ 'children' => [ [ 'country' => [ 'city' => 'New York', ] ], [ 'country' => [ 'city' => 'Sydney', ] ] ] ] ]; $result = []; try { $result['0,0,0'] = getValue($test, '0,0,0'); $result['1,0'] = getValue($test, '1,0'); $result['1,0,0'] = getValue($test, '1,0,0'); } catch (Exception $e) { echo $e->getMessage() . "\n---\n"; } var_export($result);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 21
Branch analysis from position: 21
2 jumps found. (Code = 107) Position 1 = 22, Position 2 = -2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HOkj7
function name:  (null)
number of ops:  30
compiled vars:  !0 = $test, !1 = $result, !2 = $e
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   29     0  E >   ASSIGN                                                       !0, <array>
   64     1        ASSIGN                                                       !1, <array>
   66     2        INIT_FCALL                                                   'getvalue'
          3        SEND_VAR                                                     !0
          4        SEND_VAL                                                     '0%2C0%2C0'
          5        DO_FCALL                                          0  $6      
          6        ASSIGN_DIM                                                   !1, '0%2C0%2C0'
          7        OP_DATA                                                      $6
   67     8        INIT_FCALL                                                   'getvalue'
          9        SEND_VAR                                                     !0
         10        SEND_VAL                                                     '1%2C0'
         11        DO_FCALL                                          0  $8      
         12        ASSIGN_DIM                                                   !1, '1%2C0'
         13        OP_DATA                                                      $8
   68    14        INIT_FCALL                                                   'getvalue'
         15        SEND_VAR                                                     !0
         16        SEND_VAL                                                     '1%2C0%2C0'
         17        DO_FCALL                                          0  $10     
         18        ASSIGN_DIM                                                   !1, '1%2C0%2C0'
         19        OP_DATA                                                      $10
         20      > JMP                                                          ->26
   69    21  E > > CATCH                                           last         'Exception'
   70    22    >   INIT_METHOD_CALL                                             !2, 'getMessage'
         23        DO_FCALL                                          0  $11     
         24        CONCAT                                               ~12     $11, '%0A---%0A'
         25        ECHO                                                         ~12
   72    26    >   INIT_FCALL                                                   'var_export'
         27        SEND_VAR                                                     !1
         28        DO_ICALL                                                     
         29      > RETURN                                                       1

Function getvalue:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 19
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 19
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 19
2 jumps found. (Code = 77) Position 1 = 27, Position 2 = 50
Branch analysis from position: 27
2 jumps found. (Code = 78) Position 1 = 28, Position 2 = 50
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 47
Branch analysis from position: 35
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 50
Branch analysis from position: 19
filename:       /in/HOkj7
function name:  getValue
number of ops:  53
compiled vars:  !0 = $haystack, !1 = $indexes, !2 = $indices, !3 = $finalIndex, !4 = $keys, !5 = $key, !6 = $level
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
    4     2        INIT_FCALL                                                   'explode'
          3        SEND_VAL                                                     '%2C'
          4        SEND_VAR                                                     !1
          5        DO_ICALL                                             $7      
          6        ASSIGN                                                       !2, $7
    5     7        INIT_FCALL                                                   'array_pop'
          8        SEND_REF                                                     !2
          9        DO_ICALL                                             $9      
         10        ASSIGN                                                       !3, $9
    6    11        ASSIGN                                                       !4, <array>
    7    12      > FE_RESET_R                                           $12     !2, ->19
         13    > > FE_FETCH_R                                                   $12, $13, ->19
         14    >   ASSIGN_DIM                                                   !4
         15        OP_DATA                                                      $13
    8    16        ASSIGN_DIM                                                   !4
         17        OP_DATA                                                      'children'
    7    18      > JMP                                                          ->13
         19    >   FE_FREE                                                      $12
   10    20        INIT_FCALL                                                   'array_push'
         21        SEND_REF                                                     !4
         22        SEND_VAR                                                     !3
         23        SEND_VAL                                                     'country'
         24        SEND_VAL                                                     'city'
         25        DO_ICALL                                                     
   13    26      > FE_RESET_R                                           $17     !4, ->50
         27    > > FE_FETCH_R                                           ~18     $17, !5, ->50
         28    >   ASSIGN                                                       !6, ~18
   14    29        INIT_FCALL                                                   'key_exists'
         30        SEND_VAR                                                     !5
         31        SEND_VAR                                                     !0
         32        DO_ICALL                                             $20     
         33        BOOL_NOT                                             ~21     $20
         34      > JMPZ                                                         ~21, ->47
   15    35    >   NEW                                                  $22     'Exception'
   18    36        FRAMELESS_ICALL_2                implode             ~23     '%5D%5B', !4
   20    37        ROPE_INIT                                         6  ~26     'Path+attempt+failed+for+%5B'
         38        ROPE_ADD                                          1  ~26     ~26, ~23
         39        ROPE_ADD                                          2  ~26     ~26, '%5D.+No+%60'
         40        ROPE_ADD                                          3  ~26     ~26, !5
         41        ROPE_ADD                                          4  ~26     ~26, '%60+key+found+on+levelIndex+'
         42        CAST                                              4  ~24     !6
         43        ROPE_END                                          5  ~25     ~26, ~24
         44        SEND_VAL_EX                                                  ~25
   15    45        DO_FCALL                                          0          
   20    46      > THROW                                             0          $22
   24    47    >   FETCH_DIM_R                                          ~30     !0, !5
         48        ASSIGN                                                       !0, ~30
   13    49      > JMP                                                          ->27
         50    >   FE_FREE                                                      $17
   26    51      > RETURN                                                       !0
   27    52*     > RETURN                                                       null

End of function getvalue

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
165.97 ms | 1901 KiB | 21 Q