3v4l.org

run code in 500+ PHP versions simultaneously
<?php $customer = ['name' => 'Peter', 'address' => ['street' => '5th', 'number' => 1969]]; /** * @param array $array * @param string $path A dot-separated property path. * @param mixed $default * @return mixed */ function getArrayValue(array $array, string $path, $default = null) { $parts = explode('.', $path); return array_reduce($parts, static function ($value, $part) use ($default) { return $value[$part] ?? $default; }, $array); } /** * @param string $path A dot-separated path, whose first part is a var name available in the global scope. * @param mixed $default * @return mixed */ function getGlobalArrayValue(string $path, $default = null) { @list($varName, $propertyPath) = explode('.', $path, 2); return getArrayValue($GLOBALS[$varName] ?? [], $propertyPath, $default); } echo getGlobalArrayValue('customer.name'), PHP_EOL; // Peter echo getGlobalArrayValue('customer.address.street'), PHP_EOL; // '5th' echo getGlobalArrayValue('customer.address.idontexist', 'somedefaultvalue'), PHP_EOL; // 'somedefaultvalue' echo getGlobalArrayValue('idontexist.address', 12); // 12
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/O6h2P
function name:  (null)
number of ops:  23
compiled vars:  !0 = $customer
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                       !0, <array>
   29     1        INIT_FCALL                                                   'getglobalarrayvalue'
          2        SEND_VAL                                                     'customer.name'
          3        DO_FCALL                                          0  $2      
          4        ECHO                                                         $2
          5        ECHO                                                         '%0A'
   30     6        INIT_FCALL                                                   'getglobalarrayvalue'
          7        SEND_VAL                                                     'customer.address.street'
          8        DO_FCALL                                          0  $3      
          9        ECHO                                                         $3
         10        ECHO                                                         '%0A'
   31    11        INIT_FCALL                                                   'getglobalarrayvalue'
         12        SEND_VAL                                                     'customer.address.idontexist'
         13        SEND_VAL                                                     'somedefaultvalue'
         14        DO_FCALL                                          0  $4      
         15        ECHO                                                         $4
         16        ECHO                                                         '%0A'
   32    17        INIT_FCALL                                                   'getglobalarrayvalue'
         18        SEND_VAL                                                     'idontexist.address'
         19        SEND_VAL                                                     12
         20        DO_FCALL                                          0  $5      
         21        ECHO                                                         $5
         22      > RETURN                                                       1

Function getarrayvalue:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/O6h2P
function name:  getArrayValue
number of ops:  17
compiled vars:  !0 = $array, !1 = $path, !2 = $default, !3 = $parts
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   10     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV_INIT                                            !2      null
   12     3        INIT_FCALL                                                   'explode'
          4        SEND_VAL                                                     '.'
          5        SEND_VAR                                                     !1
          6        DO_ICALL                                             $4      
          7        ASSIGN                                                       !3, $4
   13     8        INIT_FCALL                                                   'array_reduce'
          9        SEND_VAR                                                     !3
         10        DECLARE_LAMBDA_FUNCTION                              ~6      [0]
         11        BIND_LEXICAL                                                 ~6, !2
   15    12        SEND_VAL                                                     ~6
         13        SEND_VAR                                                     !0
   13    14        DO_ICALL                                             $7      
   15    15      > RETURN                                                       $7
   16    16*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/O6h2P
function name:  {closure:getArrayValue():13}
number of ops:  8
compiled vars:  !0 = $value, !1 = $part, !2 = $default
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   13     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        BIND_STATIC                                                  !2
   14     3        FETCH_DIM_IS                                         ~3      !0, !1
          4        COALESCE                                             ~4      ~3
          5        QM_ASSIGN                                            ~4      !2
          6      > RETURN                                                       ~4
   15     7*     > RETURN                                                       null

End of Dynamic Function 0

End of function getarrayvalue

Function getglobalarrayvalue:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/O6h2P
function name:  getGlobalArrayValue
number of ops:  24
compiled vars:  !0 = $path, !1 = $default, !2 = $varName, !3 = $propertyPath
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   23     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      null
   25     2        BEGIN_SILENCE                                        ~4      
          3        INIT_FCALL                                                   'explode'
          4        SEND_VAL                                                     '.'
          5        SEND_VAR                                                     !0
          6        SEND_VAL                                                     2
          7        DO_ICALL                                             $5      
          8        FETCH_LIST_R                                         $6      $5, 0
          9        ASSIGN                                                       !2, $6
         10        FETCH_LIST_R                                         $8      $5, 1
         11        ASSIGN                                                       !3, $8
         12        END_SILENCE                                                  ~4
         13        FREE                                                         $5
   26    14        INIT_FCALL                                                   'getarrayvalue'
         15        FETCH_IS                                             ~10     !2
         16        COALESCE                                             ~11     ~10
         17        QM_ASSIGN                                            ~11     <array>
         18        SEND_VAL                                                     ~11
         19        SEND_VAR                                                     !3
         20        SEND_VAR                                                     !1
         21        DO_FCALL                                          0  $12     
         22      > RETURN                                                       $12
   27    23*     > RETURN                                                       null

End of function getglobalarrayvalue

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
163.35 ms | 2036 KiB | 20 Q