如何检查数组元素是否存在?

例如:我正在检查像这样的数组元素的存在:

if (!self::$instances[$instanceKey]) { $instances[$instanceKey] = $theInstance; } 

但是,我不断收到这个错误:

 Notice: Undefined index: test in /Applications/MAMP/htdocs/mysite/MyClass.php on line 16 

当然,第一次我想要一个实例,$实例将不知道密钥。 我想我的检查可用实例是错误的?

您可以使用语言结构isset或函数array_key_exists

isset应该快一些(因为它不是函数) ,但是如果元素存在并且值为NULL ,则返回false。

例如,考虑这个数组:

 $a = array( 123 => 'glop', 456 => null, ); 

而这三项testing依赖于:

 var_dump(isset($a[123])); var_dump(isset($a[456])); var_dump(isset($a[789])); 

第一个会得到你( 元素存在,而不是null)

 boolean true 

而第二个会得到你(元素存在,但是为空)

 boolean false 

最后一个会让你(元素不存在)

 boolean false 

另一方面,像这样使用array_key_exists

 var_dump(array_key_exists(123, $a)); var_dump(array_key_exists(456, $a)); var_dump(array_key_exists(789, $a)); 

你会得到这些输出:

 boolean true boolean true boolean false 

因为在第一种情况下,元素存在 – 即使在第二种情况下它是空的。 当然,在第三种情况下,它不存在。

对于像你这样的情况,我通常使用isset ,考虑到我从来没有在第二种情况下……但select使用哪一个现在取决于你;-)

例如,你的代码可能会变成这样:

 if (!isset(self::$instances[$instanceKey])) { $instances[$instanceKey] = $theInstance; } 

与isset()相比,array_key_exists()是SLOW。 这两个组合(见下面的代码)将有所帮助。

它在保持正确的检查结果的同时,还要考虑isset()的性能优势(即使在数组元素为NULL时也返回TRUE)

 if (isset($a['element']) || array_key_exists('element', $a)) { //the element exists in the array. write your code here. } 

基准比较:(从下面的博客文章中提取)。

 array_key_exists() only : 205 ms isset() only : 35ms isset() || array_key_exists() : 48ms 

请参阅http://thinkofdev.com/php-fast-way-to-determine-a-key-elements-existance-in-an-array/和http://thinkofdev.com/php-isset-and-multi-维arrays/

进行详细的讨论。

你可以使用函数array_key_exists来做到这一点。

例如,

 $a=array("a"=>"Dog","b"=>"Cat"); if (array_key_exists("a",$a)) { echo "Key exists!"; } else { echo "Key does not exist!"; } 

PS:从这里取得的例子。

你可以使用isset()这件事情。

 $myArr = array("Name" => "Jonathan"); print (isset($myArr["Name"])) ? "Exists" : "Doesn't Exist" ; 

根据PHP手册,你可以通过两种方式来做到这一点。 这取决于你需要检查。

如果你想检查给定的键或索引是否存在于数组中,使用array_key_exists

 <?php $search_array = array('first' => 1, 'second' => 4); if (array_key_exists('first', $search_array)) { echo "The 'first' element is in the array"; } ?> 

如果你想检查数组中是否存在一个值,使用in_array

  <?php $os = array("Mac", "NT", "Irix", "Linux"); if (in_array("Irix", $os)) { echo "Got Irix"; } ?> 

你想使用array_key_exists函数。

一个小故事来说明使用array_key_exists

 // A programmer walked through the parking lot in search of his car // When he neared it, he reached for his pocket to grab his array of keys $keyChain = array( 'office-door' => unlockOffice(), 'home-key' => unlockSmallApartment(), 'wifes-mercedes' => unusedKeyAfterDivorce(), 'safety-deposit-box' => uselessKeyForEmptyBox(), 'rusto-old-car' => unlockOldBarrel(), ); // He tried and tried but couldn't find the right key for his car // And so he wondered if he had the right key with him. // To determine this he used array_key_exists if (array_key_exists('rusty-old-car', $keyChain)) { print('Its on the chain.'); } 

您也可以使用array_keys作为出现次数

 <?php $array=array('1','2','6','6','6','5'); $i=count(array_keys($array, 6)); if($i>0) echo "Element exists in Array"; ?>