Last active
August 29, 2015 14:05
-
-
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.
This file contains 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
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