3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* PathArray enables working on 'dot notation' like: $data = ['user' => ['name' => 'John', 'sex' => 'male'] ], echo PathArray::get($data, 'user.name');// (We can use different separators than '.') More example usages below the class. */ class PathArray { /* Used mostly internally, will return keys - that is, if 'path' is already an array, it will return it. if it's a string it will explode it by $separator */ public static function pathKeys ($path, $separator = '.'){ if(is_array($path)){ return $path; } return explode($separator,$path); } /* @param string|array $path a string like node1.node2.node3 or an array with keys. */ public static function exists(array $data, $path){ $result = $data; foreach(self::pathKeys($path) as $key) { if(!isset($result[$key])){ return false; } $result = $result[$key]; } return true; } /* example: PathArray::get($data, 'user.name'));//'John' @param string|array $path a string like node1.node2.node3 or an array with keys. */ public static function get(array $data, $path, $default = NULL){ $result = $data; foreach(self::pathKeys($path) as $key) { if(!isset($result[$key])){ return $default; } $result = $result[$key]; } return $result; } /* Creates a new key - value, or modifies an existing one. @param string|array $path a string like node1.node2.node3 or an array with keys. */ public static function set(array &$data, $path, $val){ $result = &$data; foreach(self::pathKeys($path) as $key) { $result = &$result[$key]; } $result = $val; return $data; } } //Example usage //Some sample data. $data = [ 'user' => ['name' => 'John', 'sex' => 'male', 'children' => ['Mary', 'Robert'] ], 'file' => ['name' => 'funny_cat'] ]; echo "--------------------::exists tests \n"; var_dump(PathArray::exists($data, 'user.name'));//true var_dump(PathArray::exists($data, 'nonexisting'));//false echo "--------------------::get tests \n"; var_dump(PathArray::get($data, 'user.name'));//'John' var_dump(PathArray::get($data, PathArray::pathKeys('user/name','/')));//different separator, also returns 'John' var_dump(PathArray::get($data, 'user.children'));//an array with two values, Mary, Robert var_dump(PathArray::get($data, 'user.children.0'));//Mary var_dump(PathArray::get($data, 'user.children.10'));//doesn't exist, default value returned (in this case - null) echo "--------------------::set tests \n"; PathArray::set($data, 'file.name', 'funny_dog');//we change existing "funny_cat" to "funny_dog" PathArray::set($data, 'file.size', '1000');//we set a new key and value var_dump($data);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kYeN9
function name:  (null)
number of ops:  71
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   ASSIGN                                                   !0, <array>
   75     1        ECHO                                                     '--------------------%3A%3Aexists+tests+%0A'
   77     2        INIT_FCALL                                               'var_dump'
          3        INIT_STATIC_METHOD_CALL                                  'PathArray', 'exists'
          4        SEND_VAR                                                 !0
          5        SEND_VAL                                                 'user.name'
          6        DO_FCALL                                      0  $2      
          7        SEND_VAR                                                 $2
          8        DO_ICALL                                                 
   78     9        INIT_FCALL                                               'var_dump'
         10        INIT_STATIC_METHOD_CALL                                  'PathArray', 'exists'
         11        SEND_VAR                                                 !0
         12        SEND_VAL                                                 'nonexisting'
         13        DO_FCALL                                      0  $4      
         14        SEND_VAR                                                 $4
         15        DO_ICALL                                                 
   80    16        ECHO                                                     '--------------------%3A%3Aget+tests+%0A'
   82    17        INIT_FCALL                                               'var_dump'
         18        INIT_STATIC_METHOD_CALL                                  'PathArray', 'get'
         19        SEND_VAR                                                 !0
         20        SEND_VAL                                                 'user.name'
         21        DO_FCALL                                      0  $6      
         22        SEND_VAR                                                 $6
         23        DO_ICALL                                                 
   83    24        INIT_FCALL                                               'var_dump'
         25        INIT_STATIC_METHOD_CALL                                  'PathArray', 'get'
         26        SEND_VAR                                                 !0
         27        INIT_STATIC_METHOD_CALL                                  'PathArray', 'pathKeys'
         28        SEND_VAL                                                 'user%2Fname'
         29        SEND_VAL                                                 '%2F'
         30        DO_FCALL                                      0  $8      
         31        SEND_VAR                                                 $8
         32        DO_FCALL                                      0  $9      
         33        SEND_VAR                                                 $9
         34        DO_ICALL                                                 
   84    35        INIT_FCALL                                               'var_dump'
         36        INIT_STATIC_METHOD_CALL                                  'PathArray', 'get'
         37        SEND_VAR                                                 !0
         38        SEND_VAL                                                 'user.children'
         39        DO_FCALL                                      0  $11     
         40        SEND_VAR                                                 $11
         41        DO_ICALL                                                 
   85    42        INIT_FCALL                                               'var_dump'
         43        INIT_STATIC_METHOD_CALL                                  'PathArray', 'get'
         44        SEND_VAR                                                 !0
         45        SEND_VAL                                                 'user.children.0'
         46        DO_FCALL                                      0  $13     
         47        SEND_VAR                                                 $13
         48        DO_ICALL                                                 
   86    49        INIT_FCALL                                               'var_dump'
         50        INIT_STATIC_METHOD_CALL                                  'PathArray', 'get'
         51        SEND_VAR                                                 !0
         52        SEND_VAL                                                 'user.children.10'
         53        DO_FCALL                                      0  $15     
         54        SEND_VAR                                                 $15
         55        DO_ICALL                                                 
   88    56        ECHO                                                     '--------------------%3A%3Aset+tests+%0A'
   90    57        INIT_STATIC_METHOD_CALL                                  'PathArray', 'set'
         58        SEND_REF                                                 !0
         59        SEND_VAL                                                 'file.name'
         60        SEND_VAL                                                 'funny_dog'
         61        DO_FCALL                                      0          
   91    62        INIT_STATIC_METHOD_CALL                                  'PathArray', 'set'
         63        SEND_REF                                                 !0
         64        SEND_VAL                                                 'file.size'
         65        SEND_VAL                                                 '1000'
         66        DO_FCALL                                      0          
   93    67        INIT_FCALL                                               'var_dump'
         68        SEND_VAR                                                 !0
         69        DO_ICALL                                                 
         70      > RETURN                                                   1

Class PathArray:
Function pathkeys:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kYeN9
function name:  pathKeys
number of ops:  11
compiled vars:  !0 = $path, !1 = $separator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      '.'
   18     2        TYPE_CHECK                                  128          !0
          3      > JMPZ                                                     ~2, ->5
   19     4    > > RETURN                                                   !0
   21     5    >   INIT_FCALL                                               'explode'
          6        SEND_VAR                                                 !1
          7        SEND_VAR                                                 !0
          8        DO_ICALL                                         $3      
          9      > RETURN                                                   $3
   22    10*     > RETURN                                                   null

End of function pathkeys

Function exists:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 16
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 16
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 13
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/kYeN9
function name:  exists
number of ops:  19
compiled vars:  !0 = $data, !1 = $path, !2 = $result, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   28     2        ASSIGN                                                   !2, !0
   29     3        INIT_STATIC_METHOD_CALL                                  'pathKeys'
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $5      
          6      > FE_RESET_R                                       $6      $5, ->16
          7    > > FE_FETCH_R                                               $6, !3, ->16
   30     8    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~7      !2, !3
          9        BOOL_NOT                                         ~8      ~7
         10      > JMPZ                                                     ~8, ->13
   31    11    >   FE_FREE                                                  $6
         12      > RETURN                                                   <false>
   33    13    >   FETCH_DIM_R                                      ~9      !2, !3
         14        ASSIGN                                                   !2, ~9
   29    15      > JMP                                                      ->7
         16    >   FE_FREE                                                  $6
   35    17      > RETURN                                                   <true>
   36    18*     > RETURN                                                   null

End of function exists

Function get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 17
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 17
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/kYeN9
function name:  get
number of ops:  20
compiled vars:  !0 = $data, !1 = $path, !2 = $default, !3 = $result, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
   43     3        ASSIGN                                                   !3, !0
   44     4        INIT_STATIC_METHOD_CALL                                  'pathKeys'
          5        SEND_VAR                                                 !1
          6        DO_FCALL                                      0  $6      
          7      > FE_RESET_R                                       $7      $6, ->17
          8    > > FE_FETCH_R                                               $7, !4, ->17
   45     9    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~8      !3, !4
         10        BOOL_NOT                                         ~9      ~8
         11      > JMPZ                                                     ~9, ->14
   46    12    >   FE_FREE                                                  $7
         13      > RETURN                                                   !2
   48    14    >   FETCH_DIM_R                                      ~10     !3, !4
         15        ASSIGN                                                   !3, ~10
   44    16      > JMP                                                      ->8
         17    >   FE_FREE                                                  $7
   50    18      > RETURN                                                   !3
   51    19*     > RETURN                                                   null

End of function get

Function set:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 12
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/kYeN9
function name:  set
number of ops:  16
compiled vars:  !0 = $data, !1 = $path, !2 = $val, !3 = $result, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   58     3        ASSIGN_REF                                               !3, !0
   59     4        INIT_STATIC_METHOD_CALL                                  'pathKeys'
          5        SEND_VAR                                                 !1
          6        DO_FCALL                                      0  $6      
          7      > FE_RESET_R                                       $7      $6, ->12
          8    > > FE_FETCH_R                                               $7, !4, ->12
   60     9    >   FETCH_DIM_W                                      $8      !3, !4
         10        ASSIGN_REF                                               !3, $8
   59    11      > JMP                                                      ->8
         12    >   FE_FREE                                                  $7
   62    13        ASSIGN                                                   !3, !2
   63    14      > RETURN                                                   !0
   64    15*     > RETURN                                                   null

End of function set

End of class PathArray.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.89 ms | 1024 KiB | 15 Q