php中有两个函数用来判断数组中是否包含指定的键,分别是array_key_exists和isset
array_key_exists语法如下
array_key_exists($key, $array)
如果键存在返回true
isset函数语法如下
isset($array[$key])
如果键存在返回true
演示代码
<?php $array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java"); print("Is 'One' defined? ".array_key_exists("One", $array)."\n"); print("Is '1' defined? ".array_key_exists("1", $array)."\n"); print("Is 'Two' defined? ".isset($array["Two"])."\n"); print("Is '2' defined? ".isset($array[2])."\n"); ?>
返回结果如下:
Is ‘One’ defined? 1 Is ’1′ defined? Is ‘Two’ defined? 1 Is ’2′ defined?