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().



array_key_exists
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;
}

?>
josh at nitrotech dot org
29-May-2006 06:24
Here is a simple method for searching nested arrays.

<?php

function in_array_multi_key($needle, $haystack)
{
  
// mutidimentional search for in_array function
   // only matches the key, values don't count.
  
if ( array_key_exists($needle, $haystack) )
   {
       return
TRUE;
   }

   foreach (
$haystack as $key => $value )
   {
       if (
is_array($value) )
       {
          
$work = in_array_multi_key($needle, $value);
           if (
$work )
           {
               return
TRUE;
           }
       }
   }
   return
FALSE;
}

?>

(Code from www.nitrotech.org)
09-May-2006 08:44
property_exists() does the same thing for object properties.

<array_intersectarray_keys>
 Last updated: Tue, 15 Nov 2005