I want to 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比较数组m,n并返回m中不包含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 ||
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.