Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: array_key_exists - Manual
[go: Go Back, main page]

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

search for in the

array_keys" width="11" height="7"/> <array_intersect
Last updated: Mon, 05 Feb 2007

view this page in

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() はオブジェクトに対しても動作します。

例 233. array_key_exists() の例

<?php
$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() です。

例 234. 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() も参照ください。



add a note add a note User Contributed Notes
array_key_exists
serkan yersen
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
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
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
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
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
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
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
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.

array_keys" width="11" height="7"/> <array_intersect
Last updated: Mon, 05 Feb 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites