Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

php

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.

like image 290
Mamadou Avatar asked Mar 25 '11 11:03

Mamadou


People also ask

How do you find the common values between two arrays?

In NumPy, we can find common values between two arrays with the help intersect1d(). It will take parameter two arrays and it will return an array in which all the common elements will appear.

How do I compare two arrays of arrays?

Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.


1 Answers

array_intersect()

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

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

var_dump($commonElements);
like image 121
Mark Baker Avatar answered Oct 21 '22 09:10

Mark Baker