[英]How to check if an array element exists?
示例:我正在檢查是否存在像這樣的數組元素:
if (!self::$instances[$instanceKey]) {
$instances[$instanceKey] = $theInstance;
}
但是,我不斷收到此錯誤:
Notice: Undefined index: test in /Applications/MAMP/htdocs/mysite/MyClass.php on line 16
當然,我第一次想要實例時,$ instances不會知道密鑰。 我想我對可用實例的檢查是錯誤的?
您可以使用語言構造isset
或函數array_key_exists
。
isset
應該更快一些(因為它不是函數) ,但是如果元素存在並且值為NULL
,則將返回false。
例如,考慮以下數組:
$a = array(
123 => 'glop',
456 => null,
);
而這三個測試,依賴於isset
:
var_dump(isset($a[123]));
var_dump(isset($a[456]));
var_dump(isset($a[789]));
第一個會得到您( 該元素存在,並且不為null) :
boolean true
雖然第二個可以幫助您(該元素存在,但為null) :
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
因為在前兩種情況下,元素存在-即使在第二種情況下為null。 而且,當然,在第三種情況下,它不存在。
對於像您這樣的情況,我通常使用isset
,考慮到我永遠不會在第二種情況下...但是現在選擇使用哪個是您的決定;-)
例如,您的代碼可能會變成這樣:
if (!isset(self::$instances[$instanceKey])) {
$instances[$instanceKey] = $theInstance;
}
與isset()相比,array_key_exists()較慢。 結合使用這兩種方法(請參見下面的代碼)會有所幫助。
它在保持正確檢查結果的同時利用了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-三維陣列/
進行詳細討論。
您可以使用函數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: 這里取了一個例子。
根據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";
}
?>
您可以為此使用isset()
。
$myArr = array("Name" => "Jonathan");
print (isset($myArr["Name"])) ? "Exists" : "Doesn't Exist" ;
您要使用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";
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.