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);
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.4.16, 8.5.0 - 8.5.1
--------------------::exists tests bool(true) bool(false) --------------------::get tests string(4) "John" string(4) "John" array(2) { [0]=> string(4) "Mary" [1]=> string(6) "Robert" } string(4) "Mary" NULL --------------------::set tests array(2) { ["user"]=> array(3) { ["name"]=> string(4) "John" ["sex"]=> string(4) "male" ["children"]=> array(2) { [0]=> string(4) "Mary" [1]=> string(6) "Robert" } } ["file"]=> array(2) { ["name"]=> string(9) "funny_dog" ["size"]=> string(4) "1000" } }
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
194.17 ms | 408 KiB | 5 Q