Skip to content

In_array() not recursive

by Shawn Stratton on August 28th, 2008

So I ran into a slight prob­lem today, I had a nested array that I needed to search for cer­tain val­ues. The prob­lem I ran into off the bat was that in_array is not recur­sive and there are no equiv­a­lents that are. I wound up search­ing the net and find­ing some ideas that I turned into a quick code snip­pet.
func­tion 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;

}

From → PHP

Comments are closed.