php检查数组下标是否存在的方法
array_key_exists()函数
array_key_exists()函数判断某个数组中是否存在指定的 key,如果该 key 存在,则返回 true,否则返回 false。
语法:
array_key_exists(key,array)
参数描述:
key 必需。规定键名。
array 必需。规定输入的数组。
实例:
<?php$a=array("a"=>"dog","b"=>"cat");if (array_key_exists("a",$a)){echo "key exists!";}else{echo "key does not exist!";}?>
输出:
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!
实例:
<?php$a=array("dog",cat");if (array_key_exists(0,$a)){echo "key exists!";}else{echo "key does not exist!";}?>
输出:
key exists!
推荐教程:php视频教程
以上就是php检查数组下标是否存在的详细内容。