Which is a better way to check if an array has more than one element?

Which is a better way to check if an array has more than one element?

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

Stefan Bogdanescu

Founder & Senior Architect

The Pitfall of Numeric Keys and How to Check Array Length

Checking if an array contains more than one element is a frequent task, yet the instinct to look for the second index often leads to subtle bugs. You might see code like if (isset($arr['1'])). While this checks whether the key '1' exists, it is logically distinct from checking if the array has more than one item. An array can easily have a numeric key of 1 while being empty of any other data, or conversely, contain only a single element at index 0. The second approach, if (sizeof($arr) > 1), checks the total count regardless of what keys are present.

The fundamental issue with using isset($arr['1']) is that it relies on specific key values rather than cardinality. In PHP, arrays can be sparse or keyed arbitrarily. If you initialize an array as $arr = [1 => 'value'], the isset check passes, but the array technically has only one element. Conversely, if you use a dense numeric index starting at zero, like $arr = ['a' => 1], the key is the string 'a', not the number 1. The isset function would return false even though the array contains exactly one item. This behavior assumes a specific structure (dense, zero-based indexing) that isn't guaranteed by PHP's definition of an array.

The traditional method using sizeof() (or its alias count()) is robust because it operates on the internal hash table size. It counts every entry added to the array, regardless of whether keys are sequential integers or strings. For instance:

$arr = ['only' => 'one'];
echo count($arr); // Outputs 1

$arr['next'] = true;
echo count($arr); // Outputs 2

This approach aligns with the semantic meaning of "how many items are in this collection?" rather than "does this specific slot exist?".

Performance Considerations

You might wonder if there is a measurable performance difference between checking count() versus inspecting keys. In modern PHP versions, arrays are implemented as hash tables where maintaining the count is an O(1) operation. This means calculating the count takes constant time relative to the size of the array. However, you should note that this internal counter must be updated whenever an element is added or removed.

While count() adds a tiny overhead compared to checking a single key with isset(), that overhead is negligible in almost all real-world scenarios. The performance cost of using count() is far outweighed by the correctness guarantees it provides. There are no standard "performance check meters" you need to run for this specific comparison because the logical failure mode of isset($arr['1']) makes it unsuitable for production code, rendering benchmarking moot. If you prioritize correctness over micro-optimizations in a conditional check, count() is the superior choice.

A Better Alternative: Counting vs. Comparing Keys

If your goal is strictly to verify that an array is not empty or has at least two items, relying on the count function is the standard practice. However, if you are working within the Laravel ecosystem, there are often collection helpers available that abstract this logic further. For example, you might check count($collection) > 1 or simply use count($arr) == 0 to detect emptiness and invert the logic.

There is a subtle edge case involving associative arrays with integer keys that are not zero-based. If you have $arr = [2 => 'two'], count($arr) correctly returns 1, but isset($arr['1']) returns false because the key is 2, not 1. This confirms that checking a specific numeric index is inherently flawed for determining size.

Conclusion on Method Selection

When judging between two alternate methods, always prioritize semantic clarity and structural independence. The count() function (or sizeof()) is independent of the keys' values, making it resilient to sparse arrays or non-sequential indices. The isset($arr['1']) pattern is fragile because it assumes a specific key mapping that does not define array size.

If you are maintaining legacy code that uses isset($arr['1']), refactor it to use count() immediately. The slight performance difference is irrelevant compared to the risk of silent logic errors where an array with one element at index 2 passes the check, or an empty array fails in unexpected ways. Stick to counting elements for size-related checks; reserve key existence checks for validating specific data points within that collection.

Our network