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']));
array_key_exists
説明
bool array_key_exists ( mixed key, array search )指定した key が配列に設定されている場合、 array_key_exists() は TRUE を返します。 key は配列添字として使用できる全ての値を使用可能です。 array_key_exists() はオブジェクトに対しても動作します。
注意: この関数の名前は、PHP 4.0.6では key_exists() です。
isset()、 array_keys() および in_array() も参照ください。
array_key_exists
brauliorossi at gmail dot com
26-Jan-2007 02:42
26-Jan-2007 02:42
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.