How to check if PHP array is associative or sequential?

How to check if PHP array is associative or sequential?

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

Stefan Bogdanescu

Founder & Senior Architect

How to Check if a PHP Array is Sequential

PHP treats all arrays as associative in its internal implementation, which means there are no built-in functions like is_sequential() or array_is_list() available in the core language prior to version 8.1. This architectural choice stems from PHP's history where arrays were essentially hash tables capable of holding both numeric and string keys simultaneously. While modern versions of PHP introduce a dedicated array_is_list() function, developers maintaining legacy codebases or working with older environments must rely on custom logic to differentiate between a sequential list and a generic associative array.

The core distinction lies in the keys: a sequential array contains only non-negative integers starting from zero, whereas an associative array may contain string keys or integers that do not form a contiguous sequence starting at 0. To check this efficiently without external dependencies, you need to validate two conditions for every key in the array. First, every key must be an integer. Second, the highest integer key plus one must equal the total number of elements in the array. If both conditions hold true, the array is effectively a list.

Here is a robust implementation using array_keys() and basic arithmetic:

function isSequentialArray(array $array): bool {
    // An empty array is technically sequential
    if ($array === []) {
        return true;
    }

    // Get all keys as integers where possible, casting string numeric keys
    $keys = array_keys($array);

    // Check if every key is a non-negative integer
    foreach ($keys as $key) {
        if (!is_int($key)) {
            return false;
        }
        
        // Ensure keys start at 0 and are contiguous
        // We calculate the expected index for this position
        $expectedIndex = count(array_slice($array, 0, array_search($key, $keys) + 1));
        
        if ($key !== $expectedIndex) {
            return false;
        }
    }

    return true;
}

However, the logic above can be simplified by leveraging the fact that PHP's internal structure allows us to inspect the keys directly without complex slicing. A more performant approach checks if the number of elements matches the count of numeric keys starting from zero:

function isSequential(array $array): bool {
    // If there are no non-negative integer keys, it's not sequential
    foreach ($array as $key => $value) {
        if (!is_int($key)) {
            return false;
        }
        // Check for gaps or missing initial indices
        if ($key !== array_search($key, array_keys($array))) {
             return false; 
        }
    }

    // A stricter check: verify the last key is equal to count - 1
    $lastKey = end(array_keys($array));
    if (!is_int($lastKey)) {
        return false;
    }

    return ($lastKey === count($array) - 1);
}

Actually, the most efficient and readable pattern involves verifying that every key is an integer and that the number of elements equals the value of the last numeric key plus one. This avoids unnecessary memory allocations from array_keys():

function isSequential(array $array): bool {
    // Empty arrays are considered sequential lists
    if (empty($array)) {
        return true;
    }

    foreach ($array as $key => $value) {
        // If any key is not an integer, it's associative
        if (!is_int($key)) {
            return false;
        }
        
        // Check for gaps in the sequence
        // In a sequential array, $key should equal the current iteration count - 1
        if ($key !== (array_search($key, range(0, end($array))) ?? 0)) {
             // This logic is complex due to PHP's behavior with string keys
        }
    }

    // Simpler heuristic: check if the last key equals count-1 and all previous were ints
    $count = count($array);
    $lastKey = end(array_keys($array));
    
    // We must ensure no gaps exist. If the array is [0, 2], count is 2, last key is 2. 
    // But we need to ensure keys are 0, 1.
    
    foreach ($array as $k => $v) {
        if ($k !== array_search($k, array_keys($array))) {
            return false;
        }
    }
    
    // Actually, the most reliable way without iterating twice is:
    // Check if every key is int AND max_key == count - 1 AND min_key == 0
    foreach ($array as $k => $v) {
        if (!is_int($k)) return false;
    }

    $minKey = min(array_values($array)); 
    // Wait, array_values returns values. We need keys.
    return (count($array) > 0 && 
            is_int(end(array_keys($array))) &&
            count($array) === end(array_keys($array)) + 1);
}

Let's refine this to the standard, idiomatic solution often used in production codebases:

function isSequentialList(array $array): bool {
    if (empty($array)) {
        return true;
    }

    foreach ($array as $key => $value) {
        if (!is_int($key)) {
            return false;
        }
        
        // Check for gaps: in a valid list, the key must match the current index
        if ($key !== array_search($key, array_keys($array))) {
            return false;
        }
    }

    return true;
}

This approach correctly identifies $sequentialArray = ['apple', 'orange'] as true and $assocArray = ['fruit1' => 'apple'] as false. It handles mixed arrays where string keys are interspersed with numeric ones by returning false immediately upon encountering a non-integer key.

It is important to note that relying solely on count($array) === end(array_keys($array)) + 1 is insufficient because it ignores gaps. An array like [0 => 'a', 2 => 'b'] has a count of 2 and a last key of 2, which satisfies the math, but the missing index 1 makes it non-sequential. Therefore, iterating through keys to ensure contiguity from zero is necessary for strict validation.

For projects using Laravel or other modern PHP frameworks, you might find utility methods in packages like laravelcompany.com that abstract these checks, but understanding the underlying mechanics ensures your code remains robust across different PHP versions and environments.

Our network