search for in the  
<array_intersectarray_keys>
Last updated: Thu, 19 May 2005

array_key_exists

(PHP 4 >= 4.1.0, PHP 5)

array_key_exists -- Checks if the given key or index exists in the array

Description

bool array_key_exists ( mixed key, array search )

array_key_exists() returns TRUE if the given key is set in the array. key can be any value possible for an array index. array_key_exists() also works on objects.

Example 1. array_key_exists() example

<?php
$search_array
= array('first' => 1, 'second' => 4);
if (
array_key_exists('first', $search_array)) {
   echo
"The 'first' element is in the array";
}
?>

Note: The name of this function is key_exists() in PHP 4.0.6.

Example 2. array_key_exists() vs isset()

isset() does not return TRUE for array keys that correspond to a NULL value, while array_key_exists() does.

<?php
$search_array
= array('first' => null, 'second' => 4);

// returns false
isset($search_array['first']);

// returns true
array_key_exists('first', $search_array);
?>

See also isset(), array_keys(), and in_array().



User Contributed Notes
array_key_exists
dave underscore bachtel ... again
11-Apr-2005 12:19
It appears that ($somearr[testindex]=== NULL) is comperable to isset() for speed (both of which are EXTREMELY faster than array_key_exists()).  Here is some code that I used for the comparisons.

<?php
//Example Code:
                  
for(some_loop_here with an increasingly larger array being searched)
                   {
                  
benchMarkThis("arraykeyexists",1);
                  
$foo = array_key_exists($newfriend,$list);
                  
benchMarkThis("arraykeyexists");

                  
benchMarkThis("eqeqeqnull",1);
                  
$foo = ($list[$newfriend] === NULL);
                  
benchMarkThis("eqeqeqnull");

                  
benchMarkThis("isset",1);
                  
$foo = isset($list[$newfriend]);
                  
benchMarkThis("isset");
                   }

  
//this function was taken from example code on php.net for the microtime function
   // return the time in seconds
  
function getmicrotime(){
       list(
$usec, $sec) = explode(" ",microtime());
       return ((float)
$usec + (float)$sec);
   }
  
  
  
//Takes in the name of a specific benchmark to track, reset parameter is an optional reset back to 0
  
function benchMarkThis($bmname, $reset=0)
   {
       static
$lastTime = array();
       static
$totalTime = array();
      
       if (
$reset == 1)    //reset the clock back to 0

      
{
          
$lastTime[$bmname] = getmicrotime();
           return;
       }
      
      
$now = getmicrotime();
      
$diff = $now - $lastTime[$bmname];
      
$diff = round($diff, 6);
      
      
$totalTime[$bmname] += $diff;
      
       echo
"$bmname time=$diff, elapsed=$totalTime[$bmname]<br>";
      
$lastTime[$bname] = $now;
   }

?>

Ouput (last iteration):
eqeqeqnull time=0.000126, elapsed=0.993481
isset time=0.000119, elapsed=0.768909
arraykeyexists time=0.011609, elapsed=52.58483

Moral of the story: if you can get away with it, do NOT use array_key_exists().
dave underscore bachtel at yahoo dot com
11-Apr-2005 01:51
After banging my head against a desk for the last few days trying to squeeze out some efficiency improvements, I began doing benchmarks for execution time using the microtime() function.

using:
array_key_exists()
34.322115

isset()
0.969917

I measure a 34x increase in performance (yes, that’s  34 times!) in my application just by substituting one function for the other.  Hard to believe!
exaton at NOSPAM dot free dot fr
14-Jan-2005 07:55
I am also noting performance gain by using isset() instead of array_key_exists().

Rudimentary tests (involving the construction of a big random array, then long testing loops working out average times) have shown a consistent 30 to 35% gain in favor of isset(), whether working with integer or string array keys, and whether the result is true or false.

The test arrays indeed did not hold any NULL values, however, and I understand that is where the issue is. It would seem therefore, that if an array is practically certain not to contain NULL values, or if that fact is irrelevant, then it is preferable to stick with isset().

I find that most counter-intuitive ; some months ago I switched my practice to array_key_exists() thinking I was doing the smart thing by using a more "dedicated" function...
dont at mail dot me
15-Dec-2004 10:35
To: nick cope nl
> array_key_exists() is in some circumstances MUCH slower than isset().

Perhaps in your case the performance gain with isset() is caused by cutting "null" values out of the recursion... got many nulls around your arrays?
nick cope nl
13-Oct-2004 07:31
array_key_exists() is in some circumstances MUCH slower than isset().

We had a case where a heavily recursive function used array_key_exists() to early exit. It went from 50 to 4 seconds when array_key_exists() was replaced with isset(). The funny thing was that with replacing the first array_key_exists() the benefit was 25% while the benefit with the second one was a factor of 20.

The timing includes the generation of the graph with jpgraph and some other work.

<array_intersectarray_keys>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT