"Undefined array key" in PHP — What It Means and How to Fix It
What the PHP error "Undefined array key" means and how to fix it.
Published October 31, 2026
"Undefined array key" occurs when code accesses an array index or key that doesn't exist (called "Undefined index" before PHP 8).
Key points
- Check the exact file and line number PHP reports — the mistake is often on or just before that line
Example
$arr = ['a' => 1];
echo $arr['b']; // Warning: Undefined array key "b"