array_key_exists

array_key_exists(string|int $key, array $array): bool

Checks whether a key exists regardless of its value — the correct answer to the classic trap where isset() returns false for keys whose value is null.

C C23

C23
#include <stdio.h>
#include <string.h>

int main(void) {
    // C has no associative array: parallel arrays + linear search
    const char *keys[] = {"name", "deleted"};
    const char *vals[] = {"Ada", NULL}; // NULL value, key present
    int n = 2, found = 0;
    for (int i = 0; i < n; i++) {
        if (strcmp(keys[i], "deleted") == 0) { found = 1; break; }
    }
    printf("%d\n", found);        // 1: the key exists even though its value is NULL
    printf("%d\n", vals[1] == NULL); // the VALUE is NULL
    return 0;
}
Output
1
1

C++ C++26

C++26
#include <iostream>
#include <map>
#include <optional>
#include <string>

int main() {
    std::map<std::string, std::optional<std::string>> a = {
        {"name", "Ada"},
        {"deleted", std::nullopt} // nullopt value, key present
    };
    std::cout << std::boolalpha;
    std::cout << a.contains("deleted") << "\n"; // true — unlike PHP isset()
    std::cout << a.contains("missing") << "\n";
    std::cout << !a["deleted"].has_value() << "\n"; // the VALUE is nullopt
    return 0;
}
Output
true
false
true

C# v11.0.100

v11.0.100
var a = new Dictionary<string, string?> {
    ["name"] = "Ada",
    ["deleted"] = null // null value, key present
};
Console.WriteLine(a.ContainsKey("deleted")); // True — unlike PHP isset()
Console.WriteLine(a.ContainsKey("missing"));
Console.WriteLine(a["deleted"] is null);     // the VALUE is null, the KEY exists
Output
True
False
True

Go v1.27.4

v1.27.4
package main

import "fmt"

func strPtr(s string) *string { return &s }

func main() {
	m := map[string]*string{"name": strPtr("Ada"), "deleted": nil} // nil value, key present
	v, ok := m["deleted"]
	fmt.Println(ok)       // true — comma-ok sees the key, unlike PHP isset()
	fmt.Println(v == nil) // the VALUE is nil
	_, ok2 := m["missing"]
	fmt.Println(ok2)
}
Output
true
true
false

Java v27.0.1

v27.0.1
import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        HashMap<String, String> a = new HashMap<>();
        a.put("name", "Ada");
        a.put("deleted", null); // null values are allowed
        System.out.println(a.containsKey("deleted")); // true — unlike PHP isset()
        System.out.println(a.containsKey("missing"));
        System.out.println(a.get("deleted") == null); // the VALUE is null, the KEY exists
    }
}
Output
true
false
true

JavaScript v26.13.1

v26.13.1
const a = { name: "Ada", deleted: null };
// 'in' also walks the prototype chain — Object.hasOwn() checks own keys only
console.log(Object.hasOwn(a, "deleted")); // true — unlike PHP isset()
console.log(Object.hasOwn(a, "missing"));
console.log("toString" in a, Object.hasOwn(a, "toString")); // true false: prototype chain
console.log(a.deleted === null); // the VALUE is null, the KEY exists
Output
true
false
true false
true

PHP v8.6.0

v8.6.0
$a = ['name' => 'Ada', 'deleted' => null];
// the classic trap: isset() is fooled by null values — array_key_exists() is not
var_dump(isset($a['deleted']));
var_dump(array_key_exists('deleted', $a));
var_dump(array_key_exists('name', $a));
var_dump(array_key_exists('missing', $a));
Output
bool(false)
bool(true)
bool(true)
bool(false)

Python v3.15.2

v3.15.2
a = {"name": "Ada", "deleted": None}
print("deleted" in a)   # True — 'in' only asks about the KEY, None values included
print("missing" in a)
print(a["deleted"] is None)  # the VALUE is None, the KEY exists
Output
True
False
True

Rust v1.99.1

v1.99.1
use std::collections::HashMap;

fn main() {
    let mut a: HashMap<&str, Option<i32>> = HashMap::new();
    a.insert("name", Some(1));
    a.insert("deleted", None); // None value, key present
    println!("{}", a.contains_key("deleted")); // true — unlike PHP isset()
    println!("{}", a.get("deleted").is_some()); // Some(None): the KEY exists
    println!("{}", a.contains_key("missing"));
}
Output
true
true
false

TypeScript v26.13.1

v26.13.1
const a: Record<string, string | null> = { name: "Ada", deleted: null };
// 'in' also walks the prototype chain — Object.hasOwn() checks own keys only
console.log(Object.hasOwn(a, "deleted")); // true — unlike PHP isset()
console.log(Object.hasOwn(a, "missing"));
console.log("toString" in a, Object.hasOwn(a, "toString")); // true false: prototype chain
console.log(a.deleted === null); // the VALUE is null, the KEY exists
Output
true
false
true false
true

Language differences

PHP core trap: isset($a['k']) returns false for a key whose value is null, while array_key_exists('k', $a) returns true; it also searches the first dimension only — nested keys are not found.

PHP 8.5: passing null as the key is deprecated (use an empty string); since 8.0 an object can no longer be passed as $array (use property_exists()).

Python: in asks about keys only — a None value does not matter, matching array_key_exists semantics.

JavaScript: Object.hasOwn() (ES2022) checks own keys; 'k' in obj also walks the prototype chain ('toString' in {} is true) — the example demonstrates the difference.

Go: the comma-ok form; a key holding a nil value still gives ok == true in v, ok := m[k] — the exact Go analogue of the PHP null trap.

Java/C#: HashMap/Dictionary allow null values, and containsKey/ContainsKey return true for such keys.

Rust: a HashMap may hold None values — both contains_key and get(...).is_some() are true (outer Some wrapping inner None); the example shows it.

C: no associative array at the language level; the example uses parallel arrays plus a linear search (a NULL value still counts as present).