Regarding performance differences between isset() and array_key_exists(), the differences may be there, but the function are not always interchangable.
Note that when $a[1] = null then isset($a[1]) == false but array_key_exists(1, $a) == true
array_key_exists
(PHP 4 >= 4.0.7, PHP 5)
array_key_exists — Prüft, ob ein Schlüssel in einem Array existiert
Beschreibung
array_key_exists() gibt TRUE zurück, wenn
key in dem Array vorhanden ist.
key kann jeder für einen Array-Index
mögliche Wert sein.
Parameter-Liste
-
key -
Der zu prüfende Wert.
-
search -
Ein Array mit den zu prüfenden Schlüsseln.
Rückgabewerte
Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.
Changelog
| Version | Beschreibung |
|---|---|
| 5.3.0 | Diese Funktion arbeitet nicht mehr mit Objekten. property_exists() sollte in diesem Fall genutzt werden. |
Beispiele
Beispiel #1 array_key_exists()-Beispiel
<?php
$search_array = array('erstes' => 1, 'zweites' => 4);
if (array_key_exists('erstes', $search_array)) {
echo "Das Element 'erstes' ist in dem Array vorhanden";
}
?>
Beispiel #2 array_key_exists() vs. isset()
isset() gibt nicht TRUE zurück für Schlüssel eines
Arrays, die zu einem NULL-Wert gehören,
array_key_exists() tut dies hingegen.
<?php
$search_array = array('erstes' => null, 'zweites' => 4);
// Gibt false zurück
isset($search_array['erstes']);
// Gibt true zurück
array_key_exists('erstes', $search_array);
?>
Anmerkungen
Hinweis:
Für die Abwärtskompatibiliät kann der folgende veraltete Alias verwendet werden: key_exists()
Siehe auch
- isset() - Prüft, ob eine Variable existiert und ob sie nicht NULL ist
- array_keys() - Liefert alle Schlüssel oder eine Teilmenge aller Schlüssel eines Arrays
- in_array() - Prüft, ob ein Wert in einem Array existiert
- property_exists() - Prüft auf die Existenz einer Eigenschaft eiens Objektes bzw. einer Klasse
array_key_exists
06-Jul-2007 05:11
11-Jun-2007 10:14
Just wondered why array_key_exists() makes me a cpu-load of 85% while isset() only needs 35%.
Not a big thing for one time execution, but in my case it have to check 1-dimensional array with ~ 15.000 entries 100 times a second. My code checks a big array for existing entrys and updates them, if needed.
Hopes it helps somebody. Notice that on many other functions, which makes coding more comfortable at the cost of speed.
28-May-2007 07:47
Seems the array_key_exists can't find a key in a multidimensional array...
Here's my fix...
<?php
function multi_array_key_exists($needle, $haystack) {
foreach ($haystack as $key=>$value) {
if ($needle==$key) {
return true;
}
if (is_array($value)) {
multi_array_key_exists($needle, $value);
}
}
return false;
}
?>
09-Apr-2007 10:58
array_key_exists is case sensitive (at least in PHP 4.3.9). To make a case-insensitive comparison you could use strtolower on both sides.
07-Mar-2007 05:01
Further research on this has turned up that the performance problems are a known, confirmed bug in PHP 5.1.x, and have been fixed in PHP builds after September 2006. You can find the bug report here: http://bugs.php.net/bug.php?id=38812
However, just because it's a fixed bug doesn't really change the conclusion. If you're writing a script and there's any chance it could be used on a PHP 5.1.x server, you should still avoid this function and use isset() or some other kind of test if you want it to run efficiently.
07-Feb-2007 01:01
marzetti.marco,
I fixed your function it's is more optimized and working better now.
function regex_array_keys($arr, $pattern){
$results[] = false;
if(!is_array($arr))
return false;
foreach($arr as $key => $val){
if(!is_array($key))
if(preg_match($pattern,$key))
array_push($results,$key);
}
return $results;
}
25-Jan-2007 06:42
Matt and mikael dot knutsson at gmail dot com:
this outputs bool(true):
$ar = array ( 'outter' => array ( 'inner' => 1 ) );
var_dump(array_key_exists('inner', $ar['outter']));
16-Dec-2006 07:50
You're right, I'm not sure what I did wrong since I had a problem where array_key_exists returned true, while
<?php
$keys = array_keys( $array );
var_dump( in_array( 'key', $keys ) );
?>
returned false. (Which does the exact same thing) I probably either messed up the array, or the order in one of the array calls.
I rewrote the entire section where I had this problem (which was probably a good idea anyway), so I don't have any demonstration code.
01-Dec-2006 10:50
mikael dot knutsson at gmail dot com:
I don't think it does, at least in PHP5?
For example, this outputs bool(false):
$ar = array ( 'outter' => array ( 'inner' => 1 ) );
var_dump(array_key_exists('inner', $ar));
So it doesn't actually check the inner array for the key 'inner'.
25-Nov-2006 01:05
When dealing with multi-dimensional arrays, this function checks through all keys in the array, including the "child arrays" unlike the array_keys( array, $search ) function which would only check and return from the first level of keys.
Took me a couple of minutes to figure out what was wrong and I hope it helps some people when looking for the right function.
03-Aug-2006 07:43
At least in PHP 4.4.0, array_key_exists is inconsistently sensitive to different data types. For example, if your first argument is a double and the keys in your array are integers, array_key_exists will always return false. If you then cast the first argument to an integer, or even to a string, then you can successfully match. I haven't tested all the possibilities, to see when it'll tolerate different data types and when it won't, so the easiest and safest solution is to cast your first argument to match the data type of the keys.
09-Jul-2006 12:25
array_key_exists() does not check values in the key i wrote this function to check if a key in an array has a empty value as corresponds to values that return true for empty() an redirects to a page if specified otherwise returns false
<?php
function keysExists($array, $startingIndex, $redirectPage) {
if(is_array($array)) {
if (!empty($startingIndex)) {
for ($i = 0; $i < $startingIndex; $i++) {
next($array);
}
}
while(list($key, $value) = each($array)) {
if (empty($value)) {
if (!empty($redirectPage)) {
header("Location: $redirectPage");
} else {
return FALSE;
}
}
}
} else {
return FALSE;
}
}
?>
email me and let me no if you found this useful!
06-Jul-2006 11:44
Returns the keys in $arr matching $pattern in a regex
<?php
function regex_array_keys ( &$arr, $pattern ) {
$results[] = false;
if( !is_array( $arr ) )
return false;
while( !is_null( $key = key( $arr ) ) ) {
if( preg_match( $pattern, $key ) )
$results[] = $key;
next($arr);
}
reset( $arr );
return $results;
}
?>
property_exists() does the same thing for object properties.