Undefined array key PHP Origin: Application & API Last updated 15 Oct 2026

Warning: Undefined array key / Undefined index

Your code reads an array key that doesn’t exist — common with $_GET, $_POST and decoded JSON.

Exact message you may see Undefined array keyUndefined indexUndefined offset
Seen on: PHP

Meaning

PHP 8 reports “Undefined array key”; PHP 7 said “Notice: Undefined index”. It’s a warning, not fatal, but the value is null and often breaks logic downstream — and printed warnings can corrupt JSON or headers.

Common causes

  • Form field not submitted (unchecked checkbox, wrong name)
  • Query parameter missing
  • API response shape differs from expectations
  • Typos in key names

⚡ Quick fix

  1. Use the null coalescing operator: $_GET['page'] ?? 1
  2. Check with isset() / array_key_exists()
  3. Validate input up front

Detailed fix by platform

PHP

  1. Defaults with ??:
    php
    $page  = (int) ($_GET['page'] ?? 1);
    $email = trim($_POST['email'] ?? '');

Code examples

Reading optional input safely

php
// ❌ Warning: Undefined array key "newsletter" when the checkbox is unchecked
$subscribe = $_POST['newsletter'];

// ✅
$subscribe = isset($_POST['newsletter']);
$page      = max(1, (int) ($_GET['page'] ?? 1));
$sort      = in_array($_GET['sort'] ?? '', ['name', 'date'], true) ? $_GET['sort'] : 'date';

Nested JSON with defaults

php
$data = json_decode($body, true) ?? [];
$city = $data['user']['address']['city'] ?? 'Unknown';   // ?? short-circuits the whole chain

How to diagnose

  1. Key — Which key and which line (from the warning)?
  2. Source — Is the field actually sent? Check the form name / query string / JSON
  3. Shape — var_dump() the array — is the structure what you expect?
  4. Guard — Use ?? defaults or isset() for optional keys

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

Array offset on null — Warning: Trying to access array offset on value of type null (PHP)Array offset on nullArrayIndexOutOfBoundsException — java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 (Java)ArrayIndexOutOfBounds…Headers already sent — Warning: Cannot modify header information - headers already sent (PHP)Headers already sentinvalid array length — RangeError: Invalid array length (JavaScript)invalid array lengthKeyError — KeyError: 'x' (Python)KeyErrorreduce empty — TypeError: Reduce of empty array with no initial value (JavaScript)reduce emptyTypeError — TypeError: Cannot read properties of undefined (reading 'x') (JavaScript)TypeErrorUndefined…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.