Default array values if key doesn't exist?

Default array values if key doesn't exist?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Handling Missing Nested Array Keys with Default Values

When working with associative arrays, especially nested structures like the one in your example, it is common to encounter scenarios where a specific key might be absent. You want to safely access $items['one']['a'] without triggering a notice or returning an empty value if the data isn't there. The core challenge is balancing safety with performance and readability while avoiding deep nesting of conditional checks.

In PHP, you can achieve this using several distinct approaches depending on your specific needs and whether you are working within the Laravel ecosystem.

The Null Coalescing Operator (??)

The most modern and readable solution for accessing a single missing key is the null coalescing operator. It returns the value if the key exists; otherwise, it returns the default.

$items = items();

// Returns 1 if 'a' exists in 'one', otherwise returns 99
echo $items['one']['a'] ?? 99; 

If the top-level key 'one' itself does not exist, PHP will throw a warning. To handle that safely without checking existence explicitly first, you can nest the operator or use a helper function:

// Returns 99 if 'one' doesn't exist OR if 'a' doesn't exist inside 'one'
echo ($items['one'] ?? [])['a'] ?? 99; 

While this syntax is concise, it can become difficult to read when dealing with deeply nested structures. If you need to default multiple levels or keys simultaneously, the syntax quickly becomes cluttered.

Using array_get from Laravel Collections

If you are already using Laravel, the most robust and readable method for deep nesting is utilizing the array_get helper function, which has been available since early versions of the framework. This allows you to pass a dot-notation path and a default value in a single, clean line:

use Illuminate\Support\Arr;

$items = items();

// Returns 99 if 'one' or 'a' is missing at any level
echo Arr::get($items, 'one.a', 99); 

This approach scales well for arbitrary depths. Unlike the null coalescing operator, array_get does not throw warnings when intermediate keys are missing; it simply returns the default value you provided. This behavior makes it ideal for handling optional configuration data or user input where strict validation hasn't occurred yet.

Using data_get in Modern Laravel Versions

For developers working with Laravel 5.1 and later, Arr::get is part of the global helper functions as data_get. You can use it directly without importing anything:

echo data_get($items, 'one.a', 99); 

This function works identically to Arr::get but offers a shorter syntax. It is particularly useful when you want to keep your codebase clean by avoiding the need for use Illuminate\Support\Arr; at the top of every file.

Trade-offs and Considerations

While default values solve the immediate problem of missing data, they can sometimes mask underlying issues in your application logic. If a key is expected to always exist based on your domain rules, returning a default like 99 might hide a bug where data is missing unexpectedly.

In production environments, it is often better to throw an exception or log a warning when a required key is missing rather than silently substituting a value. However, for optional fields—like user preferences or nullable settings—default values are the correct pattern.

If you find yourself needing default values frequently across multiple nested levels, consider refactoring your data structure to ensure consistency, or use an Eloquent model in Laravel which handles these relationships and defaults more naturally at the database level.

For further reading on accessing array values safely, LaravelCompany provides additional resources on best practices for handling data in PHP applications.

Our network