How to check if an array is associative or sequential in PHP: analyzing top mistakes
\n\nHave you ever written a foreach and gotten something unexpected? Or maybe you passed an array to a function that expected a list, but it received an associative array with string keys? If so, you are not alone. The question “How to check if PHP array is associative or sequential?” is among the top most popular on Stack Overflow with thousands of views. And this is no coincidence: confusion between array types is one of the main pitfalls for PHP developers.
In this article, we will look at the differences between associative and sequential arrays, show common mistakes, and provide working solutions. Let's go!
\n\nWhat is a sequential and associative array?
\n\nPHP does not have a strict separation into “lists” and “dictionaries” like Python or JavaScript. One type — array — can be both. But in practice, it is important to understand the difference:
- \n
- Sequential array — keys are in order: 0, 1, 2, 3... without gaps. Example:
['apple', 'pear', 'cherry']. \n - Associative array — keys are strings or numbers not in order. Example:
['name' => 'Ivan', 'age' => 30]. \n
It seems simple. But in practice, developers often make mistakes when trying to determine the array type manually. Let's analyze the most popular mistakes.
\n\nTop mistake #1: Using array_keys() and comparing with range
\n\nOne of the most common approaches is to take the array keys, generate a sequence from 0 to count($array)-1, and compare them. It looks logical, but there are nuances.
// Example of a flawed approach\nfunction isSequential($arr) {\n return array_keys($arr) === range(0, count($arr) - 1);\n}\n\n$test = ['a', 'b', 'c'];\nvar_dump(isSequential($test)); // true — correct\n\n$test2 = ['x' => 1, 'y' => 2];\nvar_dump(isSequential($test2)); // false — correct\n\n$test3 = [0 => 'a', 2 => 'b'];\nvar_dump(isSequential($test3)); // false — correct\n\n$test4 = ['0' => 'a', '1' => 'b'];\nvar_dump(isSequential($test4)); // ??? \n\n\nThe problem is that PHP automatically casts string keys to integers if they look like numbers. ['0' => 'a', '1' => 'b'] is actually a sequential array, but our function may give an unexpected result due to strict comparison (===).
Conclusion: this approach works, but only if you are sure about the key types. For production, it is better to use more reliable methods.
\n\nTop mistake #2: Checking only the first key
\n\nSome developers think: “If the first key is 0, then the array is sequential.” This is a dangerous misconception.
\n\nfunction isSequentialByFirstKey($arr) {\n if (empty($arr)) return true;\n return array_key_first($arr) === 0;\n}\n\n$bad = [0 => 'a', 'name' => 'Ivan'];\nvar_dump(isSequentialByFirstKey($bad)); // true — but should be false!\n\n\nThe first key is 0, but the array is clearly associative. Such a check will miss mixed arrays, which are common in real projects.
\n\nConclusion: never judge an array by its first element. Check all keys.
\n\nTop mistake #3: Ignoring empty arrays
\n\nAn empty array [] — is it both sequential and associative? From PHP's perspective — yes. But some functions fail.
function isAssociative($arr) {\n return count(array_filter(array_keys($arr), 'is_string')) > 0;\n}\n\nvar_dump(isAssociative([])); // false — empty array is not associative\nvar_dump(isAssociative([1, 2, 3])); // false — correct\n\n\nIt seems logical, but if you expect the function to return true for an empty array (as if it were associative), you are mistaken. An empty array is a special c