是的,array_key_exists
函數可以用于檢查關聯數組中是否存在指定的鍵
<?php
$assoc_array = array(
"name" => "John",
"age" => 30,
"city" => "New York"
);
if (array_key_exists("name", $assoc_array)) {
echo "Key 'name' exists in the associative array.";
} else {
echo "Key 'name' does not exist in the associative array.";
}
?>
在這個例子中,array_key_exists
檢查 $assoc_array
是否包含鍵 “name”。如果存在,它將輸出 “Key ‘name’ exists in the associative array.”,否則輸出 “Key ‘name’ does not exist in the associative array.”。