I read this question and it answered part of my question, but no matter what I do to modify the function, I'm not getting the expected results.
I want to pass an array to this function and check if a value of this array, exists in the multidimensional array. How can I modify this function to work if $needle
is an array
?
//example data
$needle = array(
[0] => someemail@example.com,
[1] => anotheremail@example.com,
[2] => foo@bar.com
)
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
EDIT
After reading the answer supplied by TiMESPLiNTER I updated my function as follows and it works perfectly.
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (is_array($needle) && is_array($item)) {
if(array_intersect($needle,$item)) {
return true;
}
}
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}