有没有办法检查in_array是否存在2个值?
示例:
$data = array('York','Jersey','John','Doe');
if(in_array($data == 'York') && in_array($data == 'Doe'))
{
echo '2 Values from conditional in array';
}elseif(in_array($data == 'York')){
echo echo '1 Value from conditional in array';
}elseif(in_array($data == 'None')){
echo echo 'No Value from conditional in array';
}
答案 0 :(得分:1)
这不是in_array
的工作原理,请阅读手册in_array以获得正确的语法,
if(in_array("York",$data) && in_array("Doe",$data)){
//Your code goes here
}
答案 1 :(得分:1)
See php official documentation;)
正确的语法是in_array('what you re looking', $the_array)
:
$data = array('York','Jersey','John','Doe');
if(in_array( 'York', $data) && in_array('Doe', $data))
{
echo '2 Values from conditional in array';
}
elseif(in_array('York', $data) || in_array('Doe', $data))
{
echo echo '1 Value from conditional in array';
}
else
{
echo echo 'No Value from conditional in array';
}
编辑:回答您评论的多种解决方案之一...
function both_values_exists($array, $val1, $val2)
{
if(in_array($val1, $array) && in_array($val2, $array))
return true;
else
return false;
}
$data = array('York','Jersey','John','Doe');
$res = both_values_exists($data, 'John', 'Doe');
var_dump($res); // true
答案 2 :(得分:0)
使用此代码:
$data = array('York','Jersey','John','Doe');
if(in_array('York',$data) && in_array('Doe',$data))
{
echo '2 Values from conditional in array';
} else if(in_array('York',$data)){
echo echo '1 Value from conditional in array';
} else {
echo echo 'No Value from conditional in array';
}
阅读in_array。