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 git.master, git.master_jit, rfc.property-hooks
--------------------::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" } }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
183.63 ms | 407 KiB | 5 Q