I have an array that is generated with anywhere between 3 and 12 values, it generates the array from account information;
我有一个生成3到12个值的数组,它从帐户信息生成数组;
$result = $ad->user()->groups($user['username']);
I want to check this array for multiple values (around 4 or 5) and if any of them are in it do what's inside the if, I can do it for one value pretty easily via:
我想检查这个数组是否有多个值(大约4或5),如果它们中有任何一个在if里面做什么,我可以通过以下方式很容易地为一个值做:
if (in_array("abc",$result)) { $this->login_session($user); }
Is there a simple way to check this one array for multiple values in it other than consecutive ORs:
有没有一种简单的方法来检查这个数组中的多个值,而不是连续的OR:
if (in_array("abc",$result) || in_array("123",$result) || in_array("def",$result) || in_array("456",$result) ) {
$this->login_session($user);
}
2 个解决方案
#1
16
Try and see if this is helpful:
试着看看这是否有用:
if(array_intersect($result, array('abc', '123', 'def'))) {
$this->login_session($user);
}
#2
2
This should be what you are after:
这应该是你所追求的:
$a = array(1,2,3,4,5);
$b = array(6,8);
function is_in_array($needle, $haystack) {
foreach ($needle as $stack) {
if (in_array($stack, $haystack)) {
return true;
}
}
return false;
}
var_dump(is_in_array($b, $a));
Basically loops through needle and runs an in array of it on the haystack. returns true once something is found, else it returns false.
基本上是通过针头循环并在干草堆上运行它的数组。找到某个东西后返回true,否则返回false。
#1
16
Try and see if this is helpful:
试着看看这是否有用:
if(array_intersect($result, array('abc', '123', 'def'))) {
$this->login_session($user);
}
#2
2
This should be what you are after:
这应该是你所追求的:
$a = array(1,2,3,4,5);
$b = array(6,8);
function is_in_array($needle, $haystack) {
foreach ($needle as $stack) {
if (in_array($stack, $haystack)) {
return true;
}
}
return false;
}
var_dump(is_in_array($b, $a));
Basically loops through needle and runs an in array of it on the haystack. returns true once something is found, else it returns false.
基本上是通过针头循环并在干草堆上运行它的数组。找到某个东西后返回true,否则返回false。