In_array() not recursive
So I ran into a slight problem today, I had a nested array that I needed to search for certain values. The problem I ran into off the bat was that in_array is not recursive and there are no equivalents that are. I wound up searching the net and finding some ideas that I turned into a quick code snippet.
function in_rarray($needle, $haystack, $strict = false) {
$array = new RecursiveIteratorIterator(new RecursiveArrayIterator($haystack));
foreach($array AS $element) {
if ($strict == true) {
if ($element === $needle) {
return true;
}
} else {
if($element == $needle) {
return true;
}
}
}
return false;
}


Comments are closed.