Compare two arrays, are there any common elements between two arrays?

I need to check if one element of an array is in another array.

$array_one = array("gogo", "blabla", "toto"); $array_two = array("stackov", "renaul", "toto"); 

I would like to know if one element of array_one in array_two

How to check it? Try in_array , but it has problems.

+7
source share
3 answers

array_intersect ()

 $array1 = array("gogo", "blabla", "toto"); $array2 = array("stackov","renaul","toto"); $commonElements = array_intersect($array1,$array2); var_dump($commonElements); 
+18
source

Try the following:

 array_intersect($array_one, $array_two); 
+3
source

Marking the answer should be enough for your problem. If you want to find an intersection of more than 2 arrays, use this:

 $arrays = array( array(1, 2, 3), array(2, 4, 6), array(2, 8, 16) ); $intersection = call_user_func_array('array_intersect', $arrays); 
+2
source

All Articles