array_key_exists

(PHP 4 >= 4.1.0, PHP 5)

array_key_exists -- 檢查給定的鍵名或索引是否存在於陣列中

說明

bool array_key_exists ( mixed key, array search )

array_key_exists() 在給定的 key 存在於陣列中時返回 TRUEkey 可以是任何能作為陣列索引的值。array_key_exists() 也可用於對象。

例子 1. 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()

例子 2. array_key_exists()isset() 對比

isset() 對於陣列中為 NULL 的值不會返回 TRUE,而 array_key_exists() 會。

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

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

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

參見 isset()array_keys()in_array()