Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.