0

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;
}
Community
  • 1
  • 1
Bird87 ZA
  • 2,313
  • 8
  • 36
  • 69
  • Check if [`array_intersect()`](http://php.net/manual/de/function.array-intersect.php) returns an array with at least one element. If yes one of your values in the `$needle` array is contained in the `$haystack` array. – TiMESPLiNTER Sep 16 '14 at 10:55
  • Your needle is an array and the function you have cannot cope with that. First of all you need to clarify what you want. If the needle is an array, can it be a part of an array, or should it be a whole array as such. I know this might be difficult to understand, but please think about this carefully. The first is more difficult to do than the second. – KIKO Software Sep 16 '14 at 11:12
  • Could you give us an example of your multid array? – James Lalor Sep 16 '14 at 11:17
  • PLEASE make your comment an answer @TiMESPLiNTER because that works like a bomb... – Bird87 ZA Sep 16 '14 at 11:17
  • There you go. Probably you can insert your working code in the question. – TiMESPLiNTER Sep 16 '14 at 11:44

1 Answers1

1

Check if array_intersect() returns an array with at least one element. If yes one of your values in the $needle array is contained in the $haystack array.

I ended up with this function:

function in_array_r($needle, $haystack) {
    $flatArray = array();

    array_walk_recursive($haystack, function($val, $key) use (&$flatArray) {
        $flatArray[] = $val;
    });

    return (count(array_intersect($needle, $flatArray)) > 0);
}

You may extend this function to accept multidimensional arrays for both $needle and $haystack.

CMircea
  • 3,543
  • 2
  • 36
  • 58
TiMESPLiNTER
  • 5,741
  • 2
  • 28
  • 64
  • I've updated the question with my code that works, if you want to have a read through it and maybe suggest improvements – Bird87 ZA Sep 16 '14 at 12:21