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)