Skip to content

Instantly share code, notes, and snippets.

@frogwarrior
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frogwarrior/58d0165e13542ba8afe9 to your computer and use it in GitHub Desktop.
Save frogwarrior/58d0165e13542ba8afe9 to your computer and use it in GitHub Desktop.
recursive-array-search upgraded so that it can find multiple keys which it returns as an array. If there is only one key to be found, it will return it as a variable.
public function recursive_array_search($needle,$haystack) {
$keysFound = array();
foreach($haystack as $key=>$value) {
$current_key=$key;
if($needle===$value OR is_array($value) && $this->recursive_array_search($needle,$value) !== false) {
$keysFound[] = $current_key;
}
}
if (count($keysFound) > 1) { return $keysFound; }
elseif (count($keysFound) == 1) { return $keysFound[0]; }
else { return false; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment