PHP: array_key_exists - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

array_keys array_intersect

Last updated: Sat, 27 Jan 2007

view this page in

# array_key_exists

(PHP 4 >= 4.1.0, PHP 5)

array_key_exists -- Проверить, присутствует ли в массиве указанный ключ или индекс

## Описание

bool array_key_exists ( mixed key, array input )

Функция array_key_exists() возвращает `TRUE`, если в массиве присутствует указанное значение `ключ`. Параметр `ключ` может быть любым значением, которое подходит для описания индекса массива.

Пример 1. Пример использования array_key_exists()

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

> Замечание: В PHP версии 4.0.6 название этой функции - key_exists().

См.также isset().

add a note User Contributed Notes
array_key_exists
inker2576 at yahoo dot com
06-Mar-2007 08: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.`
inker2576 at yahoo dot com
06-Mar-2007 03:11
`It's worth pointing out that this function can be very slow on large arrays, which I discovered the hard way (and I'm using PHP 5.1.6, for what it's worth). I had used this code in the core of a search engine:
<?php
...
if (array_key_exists($keyword, $SearchKeywords)) {
$keyword_stats = $SearchKeywords[$keyword];
...
}
...
?>
The $SearchKeywords array contains about 15,000 entries, and each entry is of the form: string => array. $keyword is always a string. By all rights, that code should run quickly, but to my great surprise, careful profiling showed it was taking about 10 msec each time array_key_exists() was called, and array_key_exists() was the culprit. I replaced it with this seemingly-identical code:
<?php
...
$keyword_stats = @$SearchKeywords[$keyword];
if (is_array($keyword_stats)) {
...
}
...
?>
When I replaced it, execution time went from 10 msec per keyword to 0.1 msec per keyword, a speedup of about 100 times. My guess is that array_key_exists() may use a linear search through the array, or at least some other O(n) solution. The average search time in my search engine went from about 30 msec to about 1.5 msec as a result.
In short: Don't use array_key_exists() on large arrays if you can avoid it, because it's a much slower function than it appears to be.`
serkan yersen
07-Feb-2007 04: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
25-Jan-2007 09: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
15-Dec-2006 10: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
01-Dec-2006 01: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
24-Nov-2006 04: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
03-Aug-2006 10: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 03: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 02: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;
}
?>`
09-May-2006 08:44
`property_exists() does the same thing for object properties.`
add a note
array_keys array_intersect

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST