Push only unique values into an array while looping

Push only unique values into an array while looping

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

Stefan Bogdanescu

Founder & Senior Architect

Avoiding Duplicates When Building Arrays in a Loop

The most straightforward way to filter duplicates while iterating through a collection is to rely on the built-in uniqueness properties of associative arrays. In languages like PHP, using array_unique() after the loop completes is often the lazy approach, but it requires two passes over your data and creates a new array every time. If you are working within tight memory constraints or processing large datasets where every millisecond counts, pushing duplicates into an intermediate array before filtering them out is inefficient.

A more performant strategy involves maintaining a separate tracking structure. By checking if a value already exists in your target array before adding it, you ensure that $liste grows only with unique entries. The standard in_array() function serves this purpose well. However, the naive implementation of checking existence inside a foreach loop introduces an $O(n^2)$ time complexity. As the size of $liste grows, the check becomes progressively slower because in_array() must scan the entire array on every iteration.

For modern PHP versions (7.3 and later), you can leverage the array_filter() function with a callback to achieve this in a single pass if your data structure allows it. However, when dealing with strictly sequential processing where order preservation is paramount during construction, a manual check remains the standard pattern. Here is how you might refactor the provided snippet:

$liste = [];

foreach ($something as $value) {
    // Check if value is already present before pushing
    if (!in_array($value, $liste, true)) {
        $liste[] = $value;
    }
}

The third argument in in_array() is crucial here. By passing true, you enforce a strict type check. Without it, PHP performs loose comparisons, which means the integer 1 and the string "1" would be treated as identical. Depending on your data integrity requirements, this distinction might be the difference between a correct filter and a silent bug. If your dataset mixes types or relies on exact identity, always include that flag to prevent unexpected behavior where values differ only in type are merged into the same entry.

If performance becomes a bottleneck with large datasets, consider using an associative array as a lookup table instead of scanning the list repeatedly. Since keys in an array are hashed internally, checking for existence in an associative array is $O(1)$ on average, whereas in_array() on a regular indexed array is $O(n)$.

You can achieve this by temporarily storing seen values in a temporary structure where the value itself acts as the key:

$seen = [];
$liste = [];

foreach ($something as $value) {
    // Use strict comparison for keys as well
    if (!isset($seen[$value])) {
        $seen[$value] = true;
        $liste[] = $value;
    }
}

This pattern effectively decouples the existence check from the data storage. You are now checking a hash table rather than scanning a list. While this uses slightly more memory to store the $seen keys, it drastically reduces CPU time for large inputs. If you need to preserve the original order of appearance while strictly enforcing uniqueness, this dual-array approach is often faster than re-sorting or filtering at the end.

In specific Laravel applications, you might find yourself dealing with collections that wrap these arrays. While Laravel provides helper methods like unique() on collections, understanding the underlying array mechanics helps when optimizing raw data pipelines before they hit the ORM layer. If you are building a high-throughput API endpoint where duplicate handling is critical, optimizing this loop logic can significantly reduce response times compared to relying solely on collection methods that might trigger additional internal operations.

Ultimately, the choice between in_array and an associative lookup table depends on your data volume. For small to medium-sized lists, the readability of in_array often outweighs the marginal performance gain. For large-scale data processing, the associative array pattern offers a robust solution that scales linearly rather than quadratically.

Our network