本教程操作环境:windows7系统、php8.1版、dell g3电脑
php检测数组中是否包含指定键的两种方法
1、使用array_key_exists() 函数
array_key_exists($key,$array)函数检查某个数组中是否存在指定的键名,如果键名存在则返回 true,如果键名不存在则返回 false。
<?phpheader("content-type:text/html;charset=utf-8");$a=array("volvo"=>"xc90","bmw"=>"x5");if (array_key_exists("toyota",$a)){ echo "指定键存在";}else{ echo "指定键不存在";}?>
2、使用isset() 函数
isset() 函数用于检测变量是否已设置并且非 null。
只需要使用isset() 函数检测指定数组元素$array["键名"]是否存在即可。
<?phpheader("content-type:text/html;charset=utf-8");$a=array("volvo"=>"xc90","bmw"=>"x5");if (isset($a["bmw"])){ echo "指定键存在";}else{ echo "指定键不存在";}?>
推荐学习:《php视频教程》
以上就是php怎么检测数组中是否包含指定键的详细内容。
