Mamadou
Mamadou

Reputation: 2307

Compare two arrays, is there common elements between the two arrays?

I need to test 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 is in array_two ???

How to test that? Am trying in_array but it seems to have problems.

Upvotes: 8

Views: 6878

Answers (3)

Imi Borbas
Imi Borbas

Reputation: 3703

Try this one:

array_intersect($array_one, $array_two);

Upvotes: 3

Swader
Swader

Reputation: 11597

Mark's answer should be enough for your problem. If you ever wish to find the intersect 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);

Upvotes: 2

Mark Baker
Mark Baker

Reputation: 212412

array_intersect()

$array1 = array("gogo", "blabla", "toto");
$array2 = array("stackov","renaul","toto");

$commonElements = array_intersect($array1,$array2);

var_dump($commonElements);

Upvotes: 19

Related Questions