本文介绍了php数组函数array_key_exists()的用法,array_key_exists() 函数判断某个数组中是否存在指定的 key,如果该 key 存在,则返回 true,否则返回 false,有需要的朋友参考下。
本节内容:
php数组函数之array_key_exists()
array_key_exists() 定义和用法 
array_key_exists() 函数判断某个数组中是否存在指定的 key,如果该 key 存在,则返回 true,否则返回 false。
语法 
array_key_exists(key,array) 
参数 描述 
key 必需。规定键名。 
array 必需。规定输入的数组。
例1,php数组函数之array_key_exists()。
 
复制代码 代码示例:
<?php 
$a=array("a"=>"Dog","b"=>"Cat"); 
if (array_key_exists("a",$a)) 
{ 
echo "Key exists!"; 
} 
else 
{ 
echo "Key does not exist!"; 
} 
?>
输出: 
Key exists!
例2,php数组函数之array_key_exists()。
 
复制代码 代码示例:
<?php 
$a=array("a"=>"Dog","b"=>"Cat"); 
if (array_key_exists("c",$a)) 
{ 
echo "Key exists!"; 
} 
else 
{ 
echo "Key does not exist!"; 
} 
?>
输出: 
Key does not exist!
例3,php数组函数之array_key_exists()。
 
复制代码 代码示例:
<?php 
$a=array("Dog",Cat"); 
if (array_key_exists(0,$a)) 
{ 
echo "Key exists!"; 
} 
else 
{ 
echo "Key does not exist!"; 
} 
?>
输出: 
Key exists!