Skip to content
PHP Logo

In PHP, what function can be used to check if a key exists in an array?

To check if a key exists in an array in PHP, you can use the `array_key_exists()` function. This function takes two parameters: the key you want to check for and the array you want to search in. It returns `true` if the key exists in the array and `false` otherwise. This is a simple and efficient way to determine if a specific key is present in an array.

php
// Check if a key exists in an array
$key = 'example_key';
$array = ['example_key' => 'example_value', 'another_key' => 'another_value'];

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