Reputation: 17181
How can I check that the $courseAreas
array includes at least "ayrshire"
and "fife"?
I've tried the following code, but it does not work:
$courseAreas = array('ayrshire', 'fife', 'cheshire', 'lanarkshire');
$includesAyrshireAndFife = (count(array_intersect(array('ayrshire', 'fife'), $courseAreas)) >= 2 ? true : false);
Upvotes: 0
Views: 38
Reputation: 20286
$courseAreas = array('ayrshire', 'fife', 'cheshire', 'lanarkshire');
$includesAyrshireAndFife = count(array_intersect(array('ayrshire', 'fife'), $courseAreas)) > 1;
You don't even need tenary operator because with > it's already boolean expression.
Edit:
I've noticed that your code works too. I've just shorten it.
Upvotes: 1
Reputation: 212452
Try putting the ? true : false
outside the braces
$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);
But your original also seems to work perfectly well.... in what circumstances do you find that it fails?
Upvotes: 2
Reputation: 473
You can use in_array()
See :- http://www.w3schools.com/php/func_array_in_array.asp
Upvotes: 0