Giv开发者_JS百科en a value, #some_id = x, what's the proper syntax or method to test that the id does not exist in a collection of id's?
For example...
if($some_id not in array(3, 5, 9)){
//do something
}
$theArray = array(3, 5, 9);
if(! in_array($x, $theArray)){
// do something
}
http://php.net/manual/en/function.in-array.php
in_array too slow
$array = array_flip($array);
if(!isset($array[$some_id])){ // do something }
The in_array answers are correct, but in case there's some ambiguity between the term "id" and "key", if what you're trying to do is check if a key exists, you could do either of the following:
if (array_key_exists($key, $array)) ...
or...
if (isset($array[$key])) ...
精彩评论