in_array 多个值
问题描述:
如何检查多个值,例如:
How do I check for multiple values, such as:
$arg = array('foo','bar');
if(in_array('foo','bar',$arg))
这是一个例子,所以你可以更好地理解,我知道它行不通.
That's an example so you understand a bit better, I know it won't work.
答
将目标与干草堆相交,并确保交集与目标精确相等:
Intersect the targets with the haystack and make sure the intersection is precisely equal to the targets:
$haystack = array(...);
$target = array('foo', 'bar');
if(count(array_intersect($haystack, $target)) == count($target)){
// all of $target is in $haystack
}
注意,你只需要验证结果交集的大小与目标值数组的大小相同就说明$haystack
是$target
的超集>.
Note that you only need to verify the size of the resulting intersection is the same size as the array of target values to say that $haystack
is a superset of $target
.
要验证 $target
中至少有一个值也在 $haystack
中,您可以进行以下检查:
To verify that at least one value in $target
is also in $haystack
, you can do this check:
if(count(array_intersect($haystack, $target)) > 0){
// at least one of $target is in $haystack
}