这是我的 in_array
代码
$array = array('a', 'b', 'c');
if(in_array(array('p', 'c'), $array)){
echo "found";
}else{
echo "not found";
}
它返回未找到,但实际上我希望它返回 found
,因为有一个值匹配 c
。
最佳答案
您的想法可以通过使用array_intersect
和count
函数来实现。
如果两个数组之间至少有一个匹配项 - count
将返回匹配项目的数量(1 个或更多):
$needle = array('p', 'c');
$haystack = array('a', 'b', 'c');
echo (count(array_intersect($needle, $haystack))) ? "found" : "not found";
// will output: "found"
http://php.net/manual/en/function.array-intersect.php
关于php - 如何在 php 中使用 in_array 并将数组作为针,但在至少有一个值匹配时返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38157182/