做一个PHP in_array()但是用另一个数组的针()

ude*_*ter 5 php

in_array()如果至少有一个$foo是在$bar数组中,我需要像搜索一样的东西,比如:

$foo = array("ESD", "RED", "IOP");
$bar = array("IOD", "MNP", "JDE", "RED");

if(in_array($foo, $bar)) // true because RED is in $foo and in $bar
Run Code Online (Sandbox Code Playgroud)

先感谢您!

Wes*_*rch 18

我想你想要array_intersect():

$matches = array_intersect($foo, $bar);
Run Code Online (Sandbox Code Playgroud)

$matches 将返回两个数组中所有项的数组,因此您可以:

  • 检查是否没有匹配 empty($matches)
  • 阅读匹配的数量 count($matches)
  • 如果需要,请阅读交叉点的值

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

案例示例:

$foo = array("ESD", "RED", "IOP");
$bar = array("IOD", "MNP", "JDE", "RED");

// Just cast to boolean
if ((bool) array_intersect($foo, $bar)) // true because RED is in $foo and in $bar
Run Code Online (Sandbox Code Playgroud)