in_array多个值 [英] in_array multiple values

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

推荐答案

相交草堆目标,并确保交集precisely等于目标:

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
}

请注意,您只需要验证所得的交集的尺寸大小相同的目标值数组说 $大海捞针是<$的一个超集C $ C> $目标。

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.

要验证 $大海捞针 $目标也至少有一个值,你可以这样做检查

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天全站免登陆