"John"," />

新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

php如何判断key是不是存在,php如何判断数据类型

发布时间:2024-02-29 15:50:19

php如何判断key是否是存在

在PHP中,可使用array_key_exists()函数来判断一个key是否是存在于数组中。示例以下:

$array = array("name" => "John", "age" => 30, "city" => "New York");

if (array_key_exists("name", $array)) {
    echo "Key 'name' exists in the array";
} else {
    echo "Key 'name' does not exist in the array";
}

另外,也能够使用isset()函数来判断一个key是否是存在于数组中。示例以下:

$array = array("name" => "John", "age" => 30, "city" => "New York");

if (isset($array["name"])) {
    echo "Key 'name' exists in the array";
} else {
    echo "Key 'name' does not exist in the array";
}

array_key_exists()和isset()函数的不同在于isset()函数在key存在且对应的值不为null时返回true,而array_key_exists()函数只判断key是否是存在。