0

I have a form that submits an array of user roles for processing by the server. Here is an example of the $data['roles'] posted by the form:

Array ( [0] => 5 [1] => 16 )

I want to check if $data['roles'] contains either one of the values 16, 17, 18 or 19. As you can see in the example it contains 16. in_array seems like the logical choice here, but passing an array of values as the needle of in_array doesn't work:

$special_values = array(16, 17, 18, 19);

if (in_array($special_values, $data['roles'])) {
    // this returns false
}

Neither does passing the exact same values in an array:

$special_values = array(5, 16);

if (in_array($special_values, $data['roles'])) {
    // this also returns false
}

Also, switching places between the two arrays as needle and haystack doesn't change the result. If I just ask if 16 is in the array, it works fine:

$special_value = 16;

if (in_array($special_value, $data['roles'])) {
    // this returns true
}

The documentation gives examples of using arrays as needles, however it seems the structure needs to be exactly the same in the haystack for it to return true. But then I don't get why my second example doesn't work. I'm obviously doing something wrong or missing something here.

What is the best way to check if any of the values in one array exists in another array?

Edit: This question (possible duplicate) is not asking the same thing. I want to match any value in one array against any value in another. The linked question wants to match all values in one array against the values of another.

Community
  • 1
  • 1
vtamm
  • 342
  • 1
  • 11
  • 2
    [`array_intersect`](http://php.net/manual/en/function.array-intersect.php) is what you're looking for. – Andrei Jul 02 '15 at 09:23
  • [array_intersect()](http://www.php.net/manual/en/function.array-intersect.php) might be a more logical choice – Mark Baker Jul 02 '15 at 09:23
  • possible duplicate of [in\_array but needle is array, what the alternative for it](http://stackoverflow.com/questions/2304436/in-array-but-needle-is-array-what-the-alternative-for-it) – Ben Fortune Jul 02 '15 at 09:23
  • Thanks guys. I wanted to get a true or false result and `array_intersect` gives an array of matches, but I guess I could just check `if` the result of `array_intersect` is empty and do my logic if it's not? – vtamm Jul 02 '15 at 09:26
  • @BenFortune Thank for the link, however in that question he wants to check if *all* values in the needle are in the haystack. In my example I only want to know if *any* of the values in the needle is present in the haystack. – vtamm Jul 02 '15 at 09:37
  • If you're voting this down please comment on why since it's a pretty legitimate question considering `in_array` takes arrays as needle. `array_intersect` gets me the result I want but I still need to do check on whether the result of array_intersect is empty or not to get a true or false result – so that involves an extra step compared to what I was trying to do here. – vtamm Jul 02 '15 at 09:39

2 Answers2

1

The bet way is to use array_diff and empty functions, I think:

if ( ! empty(array_diff($special_values, $data['roles']))) {
    throw new Exception('Some of special values are not in data roles');
}

or array_intersect if you want to return values which occur in both arrays.

Piotr Pasich
  • 2,639
  • 2
  • 12
  • 14
1

This may help you

Using array_intersect()

$result = array_intersect($data['roles'], array(16, 17, 18, 19));
print_r($result);

Using in_array()

$result = false;

foreach ($special_values as $val) {
    if (in_array($val, $data['roles'], true)) {
        $result = true;
    }
}
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
  • Yup, this is how I need to do it, however I only wanted `$result` to be true or false. Since `array_intersect` returns an array of matching values I'll just check if `$result` is empty or not and do my logic based on that. – vtamm Jul 02 '15 at 09:46