3v4l.org

run code in 300+ 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 = 52
Branch analysis from position: 27
2 jumps found. (Code = 78) Position 1 = 28, Position 2 = 52
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 49
Branch analysis from position: 35
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 52
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 52
Branch analysis from position: 19
filename:       /in/HOkj7
function name:  getValue
number of ops:  55
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, ->52
         27    > > FE_FETCH_R                                       ~18     $17, !5, ->52
         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, ->49
   15    35    >   NEW                                              $22     'Exception'
   16    36        INIT_FCALL                                               'sprintf'
   17    37        SEND_VAL                                                 'Path+attempt+failed+for+%5B%25s%5D.+No+%60%25s%60+key+found+on+levelIndex+%25d'
   18    38        INIT_FCALL                                               'implode'
         39        SEND_VAL                                                 '%5D%5B'
         40        SEND_VAR                                                 !4
         41        DO_ICALL                                         $23     
         42        SEND_VAR                                                 $23
   19    43        SEND_VAR                                                 !5
   20    44        SEND_VAR                                                 !6
   16    45        DO_ICALL                                         $24     
   20    46        SEND_VAR_NO_REF_EX                                       $24
   15    47        DO_FCALL                                      0          
   20    48      > THROW                                         0          $22
   24    49    >   FETCH_DIM_R                                      ~26     !0, !5
         50        ASSIGN                                                   !0, ~26
   13    51      > JMP                                                      ->27
         52    >   FE_FREE                                                  $17
   26    53      > RETURN                                                   !0
   27    54*     > RETURN                                                   null

End of function getvalue

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.63 ms | 1020 KiB | 23 Q