如果 $foo
中至少有一个在 $bar
数组中,我需要类似 in_array()
的搜索,例如:
$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
提前致谢!
最佳答案
我想你想要array_intersect()
:
$matches = array_intersect($foo, $bar);
$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
关于php - 做一个 PHP in_array() 但用一个针另一个 array(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5910665/