3v4l.org

run code in 300+ PHP versions simultaneously
<?php function setValueInNestedArray(&$array, $path, $value, $delimiter = '.'){ $pathSegments = explode($delimiter, $path); $currentNode = &$array; foreach($pathSegments as $pathSegment) { if(!is_array($currentNode[$pathSegment])) { $currentNode[$pathSegment] = []; } $currentNode = &$currentNode[$pathSegment]; } $currentNode = $value; } function getValueInNestedArray($array, $path, $defaultValue, $delimiter = '.'){ $pathSegments = explode($delimiter, $path); $pathExists = true; $currentNode = &$array; foreach($pathSegments as $pathSegment) { if(is_array($currentNode[$pathSegment])) { $currentNode = &$currentNode[$pathSegment]; } else { $pathExists = false; } } return $pathExists ? $currentNode : $defaultValue; } $arr =[]; setValueInNestedArray($arr, 'a.b.c.d', 'e'); print(1); print_r($arr); print(2); print_r(getValueInNestedArray($arr, 'a', null)); print(3); print_r(getValueInNestedArray($arr, 'a.b.c.d', null)); print(4); print_r(getValueInNestedArray($arr, 'e.f.g', null)); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jk3XN
function name:  (null)
number of ops:  38
compiled vars:  !0 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   ASSIGN                                                   !0, <array>
   32     1        INIT_FCALL                                               'setvalueinnestedarray'
          2        SEND_REF                                                 !0
          3        SEND_VAL                                                 'a.b.c.d'
          4        SEND_VAL                                                 'e'
          5        DO_FCALL                                      0          
   33     6        ECHO                                                     1
   34     7        INIT_FCALL                                               'print_r'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                                 
   35    10        ECHO                                                     2
   36    11        INIT_FCALL                                               'print_r'
         12        INIT_FCALL                                               'getvalueinnestedarray'
         13        SEND_VAR                                                 !0
         14        SEND_VAL                                                 'a'
         15        SEND_VAL                                                 null
         16        DO_FCALL                                      0  $4      
         17        SEND_VAR                                                 $4
         18        DO_ICALL                                                 
   37    19        ECHO                                                     3
   38    20        INIT_FCALL                                               'print_r'
         21        INIT_FCALL                                               'getvalueinnestedarray'
         22        SEND_VAR                                                 !0
         23        SEND_VAL                                                 'a.b.c.d'
         24        SEND_VAL                                                 null
         25        DO_FCALL                                      0  $6      
         26        SEND_VAR                                                 $6
         27        DO_ICALL                                                 
   39    28        ECHO                                                     4
   40    29        INIT_FCALL                                               'print_r'
         30        INIT_FCALL                                               'getvalueinnestedarray'
         31        SEND_VAR                                                 !0
         32        SEND_VAL                                                 'e.f.g'
         33        SEND_VAL                                                 null
         34        DO_FCALL                                      0  $8      
         35        SEND_VAR                                                 $8
         36        DO_ICALL                                                 
   41    37      > RETURN                                                   1

Function setvalueinnestedarray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 21
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 21
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 18
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/jk3XN
function name:  setValueInNestedArray
number of ops:  24
compiled vars:  !0 = $array, !1 = $path, !2 = $value, !3 = $delimiter, !4 = $pathSegments, !5 = $currentNode, !6 = $pathSegment
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      '.'
    4     4        INIT_FCALL                                               'explode'
          5        SEND_VAR                                                 !3
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $7      
          8        ASSIGN                                                   !4, $7
    6     9        ASSIGN_REF                                               !5, !0
    7    10      > FE_RESET_R                                       $10     !4, ->21
         11    > > FE_FETCH_R                                               $10, !6, ->21
    8    12    >   FETCH_DIM_R                                      ~11     !5, !6
         13        TYPE_CHECK                                  128  ~12     ~11
         14        BOOL_NOT                                         ~13     ~12
         15      > JMPZ                                                     ~13, ->18
    9    16    >   ASSIGN_DIM                                               !5, !6
         17        OP_DATA                                                  <array>
   11    18    >   FETCH_DIM_W                                      $15     !5, !6
         19        ASSIGN_REF                                               !5, $15
    7    20      > JMP                                                      ->11
         21    >   FE_FREE                                                  $10
   13    22        ASSIGN                                                   !5, !2
   14    23      > RETURN                                                   null

End of function setvalueinnestedarray

Function getvalueinnestedarray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 21
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 21
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 19
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 25
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/jk3XN
function name:  getValueInNestedArray
number of ops:  28
compiled vars:  !0 = $array, !1 = $path, !2 = $defaultValue, !3 = $delimiter, !4 = $pathSegments, !5 = $pathExists, !6 = $currentNode, !7 = $pathSegment
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      '.'
   17     4        INIT_FCALL                                               'explode'
          5        SEND_VAR                                                 !3
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $8      
          8        ASSIGN                                                   !4, $8
   19     9        ASSIGN                                                   !5, <true>
   20    10        ASSIGN_REF                                               !6, !0
   21    11      > FE_RESET_R                                       $12     !4, ->21
         12    > > FE_FETCH_R                                               $12, !7, ->21
   22    13    >   FETCH_DIM_R                                      ~13     !6, !7
         14        TYPE_CHECK                                  128          ~13
         15      > JMPZ                                                     ~14, ->19
   23    16    >   FETCH_DIM_W                                      $15     !6, !7
         17        ASSIGN_REF                                               !6, $15
         18      > JMP                                                      ->20
   25    19    >   ASSIGN                                                   !5, <false>
   21    20    > > JMP                                                      ->12
         21    >   FE_FREE                                                  $12
   28    22      > JMPZ                                                     !5, ->25
         23    >   QM_ASSIGN                                        ~18     !6
         24      > JMP                                                      ->26
         25    >   QM_ASSIGN                                        ~18     !2
         26    > > RETURN                                                   ~18
   29    27*     > RETURN                                                   null

End of function getvalueinnestedarray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.04 ms | 1407 KiB | 21 Q