key_exists()
PHP array_key_exists()函数 (PHP array_key_exists() function)
array_key_exists() function is used to check whether an array contains the given key or not. If key exists in the array – it returns true, if the key does not exist in the array – it returns false.
array_key_exists()函数用于检查数组是否包含给定键。 如果key存在于数组中-返回true ,如果key不存在于数组中-返回false 。
Syntax:
句法:
array_key_exists(key, array);
Here,
这里,
key is the value of the key to be checked.
key是要检查的密钥的值。
array is an input array, in which key will be checked.
array是一个输入数组,将在其中检查键 。
Examples:
例子:
Input:
$arr = array("name" => "Amit", "age" => 21, "city" => "Gwalior");
Output:
array_key_exists("age", $arr): true
array_key_exists("gender", $arr): false
PHP code:
PHP代码:
<?php
$arr = array("name" => "Amit", "age" => 21, "city" => "Gwalior");
//checking a key "age"
if(array_key_exists("age", $arr)){
print("array contains the key \"age\"\n");
}
else{
print("array does not contain the key \"age\"\n");
}
//checking a key "gender"
if(array_key_exists("gender", $arr)){
print("array contains the key \"gender\"\n");
}
else{
print("array does not contain the key \"gender\"\n");
}
?>
Output
输出量
array contains the key "age"
array does not contain the key "gender"
翻译自: https://www.includehelp.com/php/array_key_exists-function-with-example.aspx
key_exists()