Back to Error Center

Undefined index / Undefined array key

Error Message

Undefined array key "email"

Cause

Accessing an array key that doesn't exist. Common with request data, config values, or database results where the expected key is missing.

Solution

Use null coalescing (??), isset(), or array_key_exists() to check for key existence. Use $request->input() with a default value for request data.

Code Example

Fix
// Wrong - may trigger warning
$email = $data['email'];

// Right - use null coalescing
$email = $data['email'] ?? null;

// Or with isset
$email = isset($data['email']) ? $data['email'] : 'default';

// For request data
$email = $request->input('email', 'default@example.com');

// For config values
$debug = config('app.debug', false);

Debugging Steps

  1. Use dd() or dump() to inspect the array structure
  2. Check if the key exists with array_key_exists()
  3. Verify the data source has the expected keys
  4. Check for case sensitivity in key names

Prevention

Always use null coalescing or isset() for optional keys. Provide default values. Validate input before accessing specific keys.