Created
December 11, 2019 22:30
-
-
Save adrian-enspired/99f77686fb93397a9fbd79356567adef to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Looks up a value at given path in an array subject | |
* | |
* @param array $subject The subject | |
* @param string $path Delimited path of keys to follow | |
* @param string $delimiter Path delimiter to use (defaults to .) | |
* @return mixed The value at the given path if it exists; null otherwise | |
*/ | |
function array_dig(array $subject, string $path, string $delimiter = '.') { | |
foreach (explode($delimiter, $path) as $key) { | |
if (! isset($subject[$key])) { | |
return null; | |
} | |
$subject = $subject[$key]; | |
} | |
return $subject; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment