3

I have array like this

a1 = {1,3,5,7,9};
a2 = {2,4,6,8,9};

In both the arrays, there is a common value 9. I would like to know if there is a built in php function present which returns true or false if at least one value matches or not.

Thanks

Harshit
  • 5,147
  • 9
  • 46
  • 93
  • 7
    Take a look at `array_intersect()` – Rizier123 Nov 26 '15 at 04:09
  • As you mention this is json $a1 = {1,3,5,7,9}; $a2 = {2,4,6,8,9}; $array1 = json_decode($a1, true); $array2 = json_decode($a2, true); array_intersect($array1,$array2) ? true : false; – Hitu Bansal Nov 26 '15 at 04:16

2 Answers2

3

You can use array_intersect function in PHP. Documentation.

Example

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



$haveMatch =  (array_intersect($a, $b))?true:false;
echo $haveMatch;
jameshwart lopez
  • 2,993
  • 6
  • 35
  • 65
Tejas Jadhav
  • 93
  • 2
  • 7
2

You could use array_intersect

$a1 = [1,3,5,7,9];
$a2 = [2,4,6,8,9];
$a3 = [10];

array_intersect($a1,$a2) ? true : false;
// => true

array_intersect($a1,$a3) ? true : false;
// => false
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52