开发者

Test where ID not in (n, n1, n2)

开发者 https://www.devze.com 2023-03-06 21:00 出处:网络
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?

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])) ...
0

精彩评论

暂无评论...
验证码 换一张
取 消