Last active
April 13, 2016 13:09
-
-
Save comm1x/4e074840388c7601c317267a89ba0e8f to your computer and use it in GitHub Desktop.
PHP: get access for array via dot notation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get access to array via dot notation, for example: | |
* 'user.names.first' | |
* @param array $array Source array | |
* @param string $path Path in dot notation | |
*/ | |
function getByDotNotation($array, $path) { | |
foreach (explode('.', $path) as $part) { | |
if (! isset($array[$part])) | |
return null; | |
$array = $array[$part]; | |
} | |
return $array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment