一聚教程网:一个值得你收藏的教程网站

热门教程

PHP array_key_exists() 与array_keys() 函数使用方法

时间:2022-07-02 10:03:59 编辑:袖梨 来源:一聚教程网

PHP array_key_exists() 与array_keys() 函数使用方法与实例教程我们先来看看

array_key_exists()定义和用法
该array_key_exists ( )函数检查一个数组某一特定键,返回true ,如果存在的关键和虚假的关键是不存在。

语法

array_key_exists(key,array)
Parameter Description key Required. Specifies the key array Required. Specifies an array
提示和说明提示:请记住,如果您跳过的关键当您指定一个数组,一个整数产生的关键是开始,在0和1增加为每个价值。(见例 ) $a=array("a"=>"Dog","b"=>"Cat");
if (array_key_exists("a",$a))
 {
 echo "Key exists!";
 }
else
 {
 echo "Key does not exist!";
 }
?>
输出结果.
 
Key exists!
 
再来看个实例吧.
 
$a=array("Dog",Cat");
if (array_key_exists(0,$a))
 {
 echo "Key exists!";
 }
else
 {
 echo "Key does not exist!";
 }
?>
 
输出结果.
 
Key exists!
 
好了下面我们来接着讲array_keys() 函数使用方法
 
定义和用法该array_keys ( )函数返回一个数组包含的钥匙。 语法 array_keys(array,value)
 
Parameter Description array Required. Specifies an array value Optional. You can specify a value, then only the keys with this value are returned strict Optional. Used with the value parameter. Possible values: true - Returns the keys with the specified value, depending on type: the number 5 is not the same as the string "5".false - Default value. Not depending on type, the number 5 is the same as the string "5".
 
来看个例子.
 
$a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_keys($a));
?>
 
输出结果.
 
Array ( [0] => a [1] => b [2] => c )

热门栏目