例子:我正在检查数组元素是否存在,代码如下:
if (!self::$instances[$instanceKey]) {
$instances[$instanceKey] = $theInstance;
}
然而,我一直收到这个错误:
注意:/Applications/MAMP/htdocs/mysite/MyClass.php 的第16行中未定义索引:test
当然,在第一次想要一个实例时,$instances将不知道该键。我猜我的可用实例检查是否有误?
例子:我正在检查数组元素是否存在,代码如下:
if (!self::$instances[$instanceKey]) {
$instances[$instanceKey] = $theInstance;
}
然而,我一直收到这个错误:
注意:/Applications/MAMP/htdocs/mysite/MyClass.php 的第16行中未定义索引:test
当然,在第一次想要一个实例时,$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]));
第一个将会返回您 (元素存在且不为空) :
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()的速度较慢。以下代码结合了这两者的优点可以提高性能,同时保持正确的检查结果(即在数组元素为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-dimentional-array/以获取更详细的讨论。
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
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}
?>
<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Got Irix";
}
?>
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";
?>
isset
不安全。调用$form = [1 => 5]; var_dump(isset($from[1]));
返回false
,因为$from
不存在,而且你甚至没有收到E_NOTICE
的通知。虽然速度较慢,但更安全的是使用array_key_exists
。 - hejdav