Want to shorten this code
想要缩短这段代码
if( (in_array('2', $values)) or (in_array('5', $values)) or (in_array('6', $values)) or (in_array('8', $values)) ){
echo 'contains 2 or 5 or 6 or 8';
}
Tried this
尝试这个
(in_array(array('2', '5', '6', '8'), $values, true))
but as i understand true is only if all the values exists in array
但据我所知,只有当数组中所有的值都存在时才成立
Please, advice
请建议
4 个解决方案
#1
4
Try array_intersect()
, eg
尝试array_intersect(),如
if (count(array_intersect($values, array('2', '5', '6', '8'))) > 0) {
echo 'contains 2 or 5 or 6 or 8';
}
Example here - http://codepad.viper-7.com/GFiLGx
例子——http://codepad.viper——7. - com/gfilgx
#2
1
You can make a function like this :
你可以这样做:
function array_in_array($array_values, $array_check) {
foreach($array_values as $value)
if (in_array($value, $array_check))
return true;
return false;
}
#3
0
How about
如何
$targets = array(2,5,6,8);
$isect = array_intersect($targets, $values);
if (count($isect) != 0) {
// do stuff
}
#4
0
You can even omit count to shorten your code.
您甚至可以省略count来缩短代码。
$input = array(2,3);
if (array_intersect($input, $values)) {
echo 'contains 2 or 3';
}
#1
4
Try array_intersect()
, eg
尝试array_intersect(),如
if (count(array_intersect($values, array('2', '5', '6', '8'))) > 0) {
echo 'contains 2 or 5 or 6 or 8';
}
Example here - http://codepad.viper-7.com/GFiLGx
例子——http://codepad.viper——7. - com/gfilgx
#2
1
You can make a function like this :
你可以这样做:
function array_in_array($array_values, $array_check) {
foreach($array_values as $value)
if (in_array($value, $array_check))
return true;
return false;
}
#3
0
How about
如何
$targets = array(2,5,6,8);
$isect = array_intersect($targets, $values);
if (count($isect) != 0) {
// do stuff
}
#4
0
You can even omit count to shorten your code.
您甚至可以省略count来缩短代码。
$input = array(2,3);
if (array_intersect($input, $values)) {
echo 'contains 2 or 3';
}