25

I have two array

$a = array('a','b');
$b = array('a','1','2','3','4');

How to checks any value of array $a exists in array $b without using loop?

1 Answer 1

63
if (count(array_intersect($array1, $array2)) === 0) {
  // No values from array1 are in array 2
} else {
  // There is at least one value from array1 present in array2
}

http://php.net/manual/en/function.array-intersect.php

Probably worth nothing that, in all likelihood, under the hood, a loop is used.

Sign up to request clarification or add additional context in comments.

3 Comments

But, that loop is probably better than my loop.
Works great and nice solution.
To be fair, this statement will continue if the values of $array1 are not in $array2. You'll want to negate the comparison operator to determine if one of them is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.