Skip to content

Instantly share code, notes, and snippets.

@adrian-enspired
Created December 11, 2019 22:30
Show Gist options
  • Save adrian-enspired/99f77686fb93397a9fbd79356567adef to your computer and use it in GitHub Desktop.
Save adrian-enspired/99f77686fb93397a9fbd79356567adef to your computer and use it in GitHub Desktop.
<?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