array( 'foo' => array( 'bar' => 'You found me!' ) ) ); $vs = new ValueStore; d( $vs->deepValue('namespace.foo.bar', $data) ); d( $vs->deepValue('namespace[foo][bar]', $data) ); d( $vs->deepValue('nope', $data) ); d( $vs->deepValue('namespace', $data) ); d( $vs->deepValue('namespace.foo', $data) ); class ValueStore { public function deepValue($name, $data) { $data = (array) $data; $nameParts = $this->getNameParts($name); foreach ($nameParts as $part) { $data = empty($data[$part]) ? null : $data[$part]; if (!$data) break; } return $data; } public function getNameParts($name) { if (strpos($name, '[') !== false) { // php style preg_match_all("/([^\[\]]+)/i", $name, $matches, PREG_PATTERN_ORDER); } elseif (strpos($name, '.') !== false) { // dot notation preg_match_all("/([^\.]+)/i", $name, $matches, PREG_PATTERN_ORDER); } else { $matches[] = array($name); } return $matches[0]; } } function d($d) { ?>
array(1) { ["bar"]=> string(13) "You found me!" } } array(1) { ["bar"]=> string(13) "You found me!" } */