如何检查$courseAreas数组是否至少包括“艾尔郡”和“法夫”?
我尝试过以下代码,但它不起作用:
$courseAreas = array('ayrshire', 'fife', 'cheshire', 'lanarkshire');
$includesAyrshireAndFife = (count(array_intersect(array('ayrshire', 'fife'), $courseAreas)) >= 2 ? true : false);发布于 2015-11-27 12:12:59
$courseAreas = array('ayrshire', 'fife', 'cheshire', 'lanarkshire');
$includesAyrshireAndFife = count(array_intersect(array('ayrshire', 'fife'), $courseAreas)) > 1;您甚至不需要张力运算符,因为with >它已经是布尔表达式了。
编辑:
我注意到你的代码也起作用了。我只是把它缩短了。
发布于 2015-11-27 12:11:28
试着把? true : false放在大括号外面
$courseAreas = array('ayrshire', 'fife', 'cheshire', 'lanarkshire');
$includesAyrshireAndFife = (count(array_intersect(array('ayrshire', 'fife'), $courseAreas)) >= 2) ? true : false;
var_dump($includesAyrshireAndFife);
$courseAreas = array('ayrshire', 'stirlingshire', 'cheshire', 'lanarkshire');
$includesAyrshireAndFife = (count(array_intersect(array('ayrshire', 'fife'), $courseAreas)) >= 2) ? true : false;
var_dump($includesAyrshireAndFife);但你的原作似乎也很好.在什么情况下你发现它失败了?
发布于 2015-11-27 12:08:25
您可以使用in_array()
见:- array.asp
https://stackoverflow.com/questions/33957049
复制相似问题