array_key_exists(), at least in 5.2.4, passes the array by value. I conclude this from seeing performance worsen as the array to search got bigger. isset() doesn't have this problem.
array_key_exists
(PHP 4 >= 4.0.7, PHP 5)
array_key_exists — 指定したキーまたは添字が配列にあるかどうかを調べる
説明
bool array_key_exists ( mixed $key, array $search )指定した key が配列に設定されている場合、 array_key_exists() は TRUE を返します。 key は配列添字として使用できる全ての値を使用可能です。 array_key_exists() はオブジェクトに対しても動作します。
パラメータ
- key
調べる値。
- search
キーが存在するかどうかを調べたい配列。
返り値
成功した場合に TRUE を、失敗した場合に FALSE を返します。
例
例 250. array_key_exists() の例
<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "この配列には 'first' という要素が存在します";
}
?>
注意: この関数の名前は、PHP 4.0.6では key_exists() です。
例 251. array_key_exists() 対 isset()
isset() は NULL 値を持つ配列キーに対して TRUE を返しません。一方、array_key_exists() は TRUE を返します。
<?php
$search_array = array('first' => null, 'second' => 4);
// false を返します
isset($search_array['first']);
// true を返します
array_key_exists('first', $search_array);
?>
参考
| isset() |
| array_keys() |
| in_array() |
array_key_exists
wolf550e at gmail dot com
28-Sep-2007 04:51
28-Sep-2007 04:51
diogoshaw at gmail dot com
16-Sep-2007 03:58
16-Sep-2007 03:58
this function very good to use if you need to verify many variables:
<?php
function array_key_exists_r($keys, $search_r) {
$keys_r = split('\|',$keys);
foreach($keys_r as $key)
if(!array_key_exists($key,$search_r))
return false;
return true;
}
?>
e.g.
<?php
if(array_key_exists_r('login|user|passwd',$_GET)) {
// login
} else {
// other
}
?>
works for me, enjoy.
dg shaw.
j_hattersleydykes {at} yahoo uk
27-Aug-2007 09:39
27-Aug-2007 09:39
hey - I thought this function maybe useful to someone somewhere..
It works on an array of the keys you want to check exist. you could pass in the names of form fields and the POST array - suppose it could be useful in aiding form validation.
function array_keys_exist(array $keys, array $toCheck, $whichKey = false)
{
foreach ($keys as $array_key)
{
if (! array_key_exists($array_key, $toCheck))
{
// return first key thats not found.
if ($whichKey)
{
return $array_key;
}
else
{
return false;
}
}
}
// all keys exist
return true;
}
hope someone finds it useful :)
sj-b at hotmail dot de
01-Aug-2007 01:14
01-Aug-2007 01:14
i dont like how empty() works.
an integer with value 0 or a boolean wth
value false (same like zero) counts as
empty too.
[code]function r_empty (&$check)
{
if (!isset($check)) return true;
if ($check == NULL) return true;
return false;
}[/code]
that is a good replacement for
both functions for me.
Lucknut dot xbl at googlemail dot com
19-Jul-2007 02:44
19-Jul-2007 02:44
I found this function very good to use if your want your urls like index.php?login or index.php?register
e.g.
<?php
if( array_key_exists( 'home',$_GET ) ) {
echo "Home - its where the heart is.";
} else if( array_key_exists( 'login',$_GET ) ) {
echo "Login code here!";
} else if( array_key_exists( 'register',$_GET ) ) {
echo "Register code here!";
} else {
echo "Home - its where the heart is.";
}
?>
david at madole dot net
06-Jul-2007 12:11
06-Jul-2007 12:11
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
eidberger at jakota dot de
12-Jun-2007 05:14
12-Jun-2007 05: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.
alishahnovin at hotmail dot com
29-May-2007 02:47
29-May-2007 02: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;
}
?>
php at ianco dot co dot uk
10-Apr-2007 05:58
10-Apr-2007 05: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.
inker2576 at yahoo dot com
07-Mar-2007 01:01
07-Mar-2007 01: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.
serkan yersen
07-Feb-2007 09:01
07-Feb-2007 09: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;
}
brauliorossi at gmail dot com
26-Jan-2007 02:42
26-Jan-2007 02: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']));
mikael dot knutsson at gmail dot com
16-Dec-2006 03:50
16-Dec-2006 03: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.
Matt
02-Dec-2006 06:50
02-Dec-2006 06: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'.
mikael dot knutsson at gmail dot com
25-Nov-2006 09:05
25-Nov-2006 09: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.
Mike Toppa
04-Aug-2006 02:43
04-Aug-2006 02: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.
ncurtis at cenicola-helvin dot com
09-Jul-2006 07:25
09-Jul-2006 07: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!
marzetti.marco at gmail.com
06-Jul-2006 06:44
06-Jul-2006 06: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;
}
?>
10-May-2006 12:44
property_exists() does the same thing for object properties.