How to check if an associative array has an empty or null value

How to check if an associative array has an empty or null value

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

Stefan Bogdanescu

Founder & Senior Architect

Distinguishing Between Empty, Null, and Zero in PHP Arrays

Determining whether a specific key in an associative array holds an empty or null value is a common requirement, yet it often leads to subtle bugs if the definition of "empty" isn't strictly enforced. Consider the following array structure:

$array = [
    0 => 0,
    1 => 1,
    2 => '',
    3 => 2,
    4 => null
];

In this example, index 0 holds a valid integer zero, index 2 holds an empty string, and index 4 holds a null value. The goal is to identify indices 2 and 4 while treating index 0 as a legitimate, non-empty value.

The most frequent approach developers take is using the empty() superglobal function. While convenient, this method conflates distinct concepts. In PHP, empty($value) returns true if the value is false (like 0 or ""), null, an empty string, an empty array, or undefined. Consequently, checking if(empty($array[$key])) fails immediately for numeric zeros because it treats them as falsy values rather than valid data. If your logic requires zero to be a valid entry, empty() is insufficient without additional checks.

A slightly more robust alternative often seen in forums involves combining isset() with a strict comparison: if(isset($array[$value]) && $array[$value] !== ''). This approach correctly handles the zero case because isset() returns false only if the key does not exist or is explicitly set to null (in older PHP versions) or if the value is unset. However, this pattern has a specific limitation: it only checks against an empty string. It will return true for index 4 (null) because isset() technically returns false for null values in most contexts, but the logic becomes brittle if you need to distinguish between null and other falsy types explicitly.

The most precise solution relies on strict type checking. To determine if a value is effectively "missing" or strictly null, use is_null(). If your definition of empty includes empty strings but excludes zero, you can create a helper condition:

function hasValue($array, $key) {
    return isset($array[$key]) && $array[$key] !== null && $array[$key] !== '';
}

This function ensures that the key exists (isset), is not null (is_null implicitly handled by !== null), and is not an empty string. If you need to treat zero as a valid value, this logic holds up perfectly. It distinguishes between "no data" (null/empty string) and "data with a falsy value" (0).

In frameworks like Laravel, you might encounter similar scenarios when validating request inputs or checking database relationships. When dealing with user input, request()->input('field') often returns null if the key is missing from the POST array. Using is_null() here provides clarity over relying on truthy/falsy evaluation, preventing logic errors where a missing field is treated as a zero value unexpectedly.

Ultimately, the choice depends on your semantic definition of "empty." If zero represents an absence of quantity rather than data, empty() is dangerous. If you are cleaning up data streams and want to strip out nulls and empty strings but keep zeros, strict inequality checks against null and '' provide the necessary granularity without the side effects of type juggling inherent in loose comparisons.

Our network