2009-12-27 24 views
47

उदाहरण: मैं इस तरह एक सरणी तत्व के अस्तित्व के लिए जाँच कर रहा हूँ:यह जांचने के लिए कि कोई सरणी तत्व मौजूद है या नहीं?

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

हालांकि, मैं इस त्रुटि प्राप्त हो रही:

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

, पहली बार मैं एक उदाहरण चाहते हैं , $ उदाहरण कुंजी को नहीं पता होगा। मुझे लगता है कि उपलब्ध उदाहरण के लिए मेरा चेक गलत है?

उत्तर

86

आप या तो भाषा का निर्माण isset, या फ़ंक्शन array_key_exists का उपयोग कर सकते हैं।

isset होना चाहिए थोड़ा तेजी से (यह एक समारोह नहीं है), लेकिन अवास्तविक लौटाते हैं, तो तत्व मौजूद है और मूल्य NULL है।


उदाहरण के लिए , इस सरणी पर विचार:

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

और उन तीन परीक्षणों, isset पर निर्भर:

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

पहले एक आप मिल जाएगा (तत्व मौजूद है, और है शून्य नहीं):

boolean true 
,210

दूसरा एक तुम (तत्व मौजूद है, लेकिन रिक्त है) मिल जाएगा जबकि:

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)); 

आप उन outputs मिल चाहते हैं:

boolean true 
boolean true 
boolean false 

वजह से, दो प्रथम मामलों में, तत्व मौजूद है - भले ही वह दूसरे मामले में अशक्त है। और, ज़ाहिर है, तीसरे मामले में, यह अस्तित्व में नहीं है।


ऐसे तुम्हारा के रूप में स्थितियों के लिए, मैं आम तौर पर isset उपयोग करते हैं, पर विचार मैं दूसरे मामले में कभी नहीं कर रहा हूँ ... लेकिन जो एक का उपयोग करने को चुनने के लिए अब आप ;-)

उदाहरण के लिए निर्भर है,

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

मुझे शिकायत करना है कि 'कारण' जारी करना टाइप-असुरक्षित है। '$ Form = [1 => 5] कहा जाता है; var_dump (जारीकर्ता ($ 1 से $)); '' से '' '' '' '' '' 'से' '' __OTICE' द्वारा अधिसूचित नहीं किया गया है। धीमे, लेकिन सुरक्षित 'array_key_exists' मेरे लिए चीज करते हैं। – hejdav

4

आप array_key_exists फ़ंक्शन का उपयोग करना चाहते हैं।

6

आप इस बात के लिए isset() का उपयोग कर सकते हैं।

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

आप ऐसा करने के लिए array_key_exists फ़ंक्शन का उपयोग कर सकते हैं।

उदाहरण के लिए,

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

पुनश्च: उदाहरण here से लिया।

23

array_key_exists() धीमा isset की तुलना में है(): अपने कोड कुछ इस तरह बन सकता है। इन दोनों का संयोजन (नीचे कोड देखें) मदद करेगा।

यह) isset का प्रदर्शन लाभ (जबकि सही जाँच परिणाम को बनाए रखने (अर्थात लौट सही भी जब सरणी तत्व शून्य है)

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-dimentional-array/

देखें।

+0

और, * जारी * भी भ्रामक है। "सेट सेट" नाम का एक कीवर्ड क्यों होगा * झूठा * जब एक चर या एक सरणी स्थिति वास्तव में सेट की जाती है - भले ही यह * शून्य * पर सेट हो? –

+0

सबसे परिष्कृत उत्तर! – Hafenkranich

5

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"; 
} 
?> 
1

तुम भी घटनाओं की संख्या के लिए 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"; 
?> 
2

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.'); 
} 
संबंधित मुद्दे

 संबंधित मुद्दे