Associative arrays use string (or int) keys—like JavaScript objects or Python dicts. They model records, config maps, and lookup tables.
Syntax
$user = [
'id' => 1,
'name' => 'Ada',
'email' => 'ada@example.com',
];
echo $user['name'];
Checking keys
array_key_exists('email', $user) or isset($user['email'])—isset returns false if the value is null.
Mixed arrays
PHP allows numeric and string keys in one array, but keep structures predictable for teammates and static analysis tools.
Important interview questions and answers
- Q: Array vs object in PHP?
A: Arrays are built-in ordered maps; objects are instances of classes with methods and visibility—use objects when behavior belongs with data.
Self-check
- How do you read the
emailkey safely if it might be missing? - Can associative arrays preserve insertion order?