php in_array 多个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7542694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:55:23  来源:igfitidea点击:

in_array multiple values

phparrays

提问by daryl

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.

这是一个例子,所以你可以更好地理解,我知道它行不通。

回答by Mark Elliot

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
}

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 $haystackis a superset of $target.

请注意,您只需要验证生成的交集的大小与目标值数组的大小相同,即表示它$haystack是 的超集$target

To verify that at least one value in $targetis also in $haystack, you can do this check:

要验证至少一个值 in$target也是 in $haystack,您可以执行以下检查:

 if(count(array_intersect($haystack, $target)) > 0){
     // at least one of $target is in $haystack
 }

回答by Rok Kralj

As a developer, you should probably start learning set operations (difference, union, intersection). You can imagine your array as one "set", and the keys you are searching for the other.

作为开发人员,您可能应该开始学习集合操作(​​差、并、交)。你可以把你的数组想象成一个“集合”,而你正在寻找另一个的键。

Check if ALL needles exist

检查所有针是否存在

function in_array_all($needles, $haystack) {
   return empty(array_diff($needles, $haystack));
}

echo in_array_all( [3,2,5], [5,8,3,1,2] ); // true, all 3, 2, 5 present
echo in_array_all( [3,2,5,9], [5,8,3,1,2] ); // false, since 9 is not present

Check if ANY of the needles exist

检查是否存在任何针

function in_array_any($needles, $haystack) {
   return !empty(array_intersect($needles, $haystack));
}

echo in_array_any( [3,9], [5,8,3,1,2] ); // true, since 3 is present
echo in_array_any( [4,9], [5,8,3,1,2] ); // false, neither 4 nor 9 is present

回答by RiaD

if(in_array('foo',$arg) && in_array('bar',$arg)){
    //both of them are in $arg
}

if(in_array('foo',$arg) || in_array('bar',$arg)){
    //at least one of them are in $arg
}

回答by sMyles

Going off of @Rok Kralj answer (best IMO) to check if any of needles exist in the haystack, you can use (bool)instead of !!which sometimes can be confusing during code review.

从@Rok Kralj 答案(最佳 IMO)开始检查大海捞针中是否存在任何针,您可以使用它(bool)来代替!!,有时在代码期间可能会令人困惑。

function in_array_any($needles, $haystack) {
   return (bool)array_intersect($needles, $haystack);
}

echo in_array_any( array(3,9), array(5,8,3,1,2) ); // true, since 3 is present
echo in_array_any( array(4,9), array(5,8,3,1,2) ); // false, neither 4 nor 9 is present

https://glot.io/snippets/f7dhw4kmju

https://glot.io/snippets/f7dhw4kmju

回答by maraspin

IMHO Mark Elliot's solution's best one for this problem. If you need to make more complex comparison operations between array elements AND you're on PHP 5.3, you might also think about something like the following:

恕我直言,马克埃利奥特的解决方案是这个问题的最佳解决方案。如果您需要在数组元素之间进行更复杂的比较操作并且您使用的是 PHP 5.3,您可能还会考虑以下内容:

<?php

// First Array To Compare
$a1 = array('foo','bar','c');

// Target Array
$b1 = array('foo','bar');


// Evaluation Function - we pass guard and target array
$b=true;
$test = function($x) use (&$b, $b1) {
        if (!in_array($x,$b1)) {
                $b=false;
        }
};


// Actual Test on array (can be repeated with others, but guard 
// needs to be initialized again, due to by reference assignment above)
array_walk($a1, $test);
var_dump($b);

This relies on a closure; comparison function can become much more powerful. Good luck!

这依赖于一个闭包;比较功能可以变得更加强大。祝你好运!

回答by Umesh Patil

if(empty(array_intersect([21,22,23,24], $check_with_this)) {
 print "Not found even a single element";
} else {
 print "Found an element";
}

array_intersect()returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.

array_intersect()返回一个数组,其中包含所有参数中出现的 array1 的所有值。请注意,密钥被保留。

Returns an array containing all of the values in array1 whose values exist in all of the parameters.

返回一个包含 array1 中所有值的数组,这些值存在于所有参数中。



empty() —Determine whether a variable is empty

empty() —判断变量是否为空

Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

如果 var 存在且具有非空、非零值,则返回 FALSE。否则返回 TRUE。