Check if more than one value is in an array
I want t开发者_如何学Co check if the numbers 7, 86 and 99 exist in a array called $category
.
So far I have this, but I don't want to use three lines to do it:
if (in_array("7", $category)) { //do something }
$search = array("7", "86", "99");
If the ids are the keys of the $category
variable:
if (count(array_intersect($search, array_keys($category))) == count($search)) {
// all found
}
if (count(array_intersect($search, array_keys($category))) > 0) {
// some found
}
If the ids are the values of the $category
variable:
if (count(array_intersect($search, $category)) == count($search)) {
// all found
}
if (count(array_intersect($search, $category)) > 0) {
// some found
}
array_diff compares arrays m,n and returns any elements of m not in n.
count( array_diff( array(7,86,99), $category ) )
if (in_array("7", $category) + in_array("86", $category) + in_array("99", $category) >= 2)
echo "at least two is exist in the array";
Update: use >= 1
or ||
精彩评论