How to check if an array is associative or not in PHP: top mistakes

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to check if an array is associative or sequential in PHP: analyzing top mistakes

\n\n

Have 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.

\n\n

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\n

What is a sequential and associative array?

\n\n

PHP 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\n
    \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
\n\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\n

Top mistake #1: Using array_keys() and comparing with range

\n\n

One 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.

\n\n
// 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\n

The 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 (===).

\n\n

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\n

Top mistake #2: Checking only the first key

\n\n

Some developers think: “If the first key is 0, then the array is sequential.” This is a dangerous misconception.

\n\n
function 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\n

The first key is 0, but the array is clearly associative. Such a check will miss mixed arrays, which are common in real projects.

\n\n

Conclusion: never judge an array by its first element. Check all keys.

\n\n

Top mistake #3: Ignoring empty arrays

\n\n

An empty array [] — is it both sequential and associative? From PHP's perspective — yes. But some functions fail.

\n\n
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\n

It 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

Blogs

Book Recommendations