in_array 多个值 [英] in_array multiple values

查看:32
本文介绍了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
 }

这篇关于in_array 多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆