How to remove duplicate values from an array in PHP

How to remove duplicate values from an array in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

How to remove duplicate values from an array in PHP

Removing duplicates from a collection is one of the most frequent array manipulation tasks in PHP development. Depending on the strictness of your requirements, there are several distinct approaches to solving this problem, each carrying specific performance implications and edge case behaviors. The simplest method relies on casting the array to a string or using built-in functions, but understanding the underlying mechanics ensures you choose the right tool for production environments.

Using array_unique

The most direct solution is the array_unique() function. This function traverses the input array and removes duplicate values while preserving the first occurrence of each value.

$data = ['apple', 'banana', 'apple', 'cherry', 'banana'];
$uniqueData = array_unique($data);

print_r($uniqueData);
// Output: Array ( [0] => apple [1] => banana [2] => cherry )

Under the hood, array_unique() converts all elements to strings before performing comparisons. This behavior is critical when dealing with numeric keys or type juggling. For instance, if your array contains integers and strings that look identical, such as 1 and "1", they will be treated as duplicates by this function because of the string conversion step.

$mixed = [1, "1", 2];
print_r(array_unique($mixed));
// Output: Array ( [0] => 1 [1] => 2 ) 
// Note: The integer 1 and string "1" are merged into one value.

This function is efficient for simple scenarios but requires caution when type safety is paramount. It also retains the original keys of the array, which might lead to gaps if you later expect a dense index starting from zero. In many cases, using array_values() immediately after array_unique() is necessary to reindex the result numerically.

Using array_combine with array_map

When type preservation is essential and you cannot afford the string conversion of array_unique(), you must compare values while maintaining their original types. A robust approach involves iterating through the array and building a new one based on a key that represents the unique value itself, or checking existence before insertion.

$data = [1, "1", 2];
$uniqueData = [];

foreach ($data as $value) {
    // Check if this specific value already exists in our result array
    if (!array_key_exists((string)$value, $uniqueData)) {
        $uniqueData[(string)$value] = $value;
    }
}

// Reindex keys to be 0, 1, 2...
$uniqueData = array_values($uniqueData);
print_r($uniqueData);

While this manual iteration offers granular control over type handling, it is inherently slower than array_unique() for large datasets because it performs linear lookups on the growing result set. However, for arrays where the distinction between 1 and "1" matters, this trade-off is acceptable.

Performance Considerations and Large Datasets

If you are processing massive arrays or streaming data, memory efficiency becomes a concern. Functions like array_unique() load the entire array into memory to perform comparisons. For extremely large datasets where memory footprint is a constraint, algorithms that sort the data first (O(n log n)) or use hash maps with O(n) complexity are theoretically superior, though PHP's internal optimizations often make array_unique() fast enough for typical web application payloads.

In Laravel applications, you might find yourself needing this functionality within collection chains. While collections provide a fluent API, relying on native functions like unique() is often the most performant choice for raw arrays. However, if you are working with Eloquent query results or complex data structures where uniqueness includes associated relationships rather than just column values, you may need to leverage database constraints or specific query modifiers instead of client-side array manipulation.

For projects requiring deeper collection logic, resources like LaravelCompany often highlight best practices for optimizing data processing within the ecosystem, emphasizing when to offload heavy lifting to the database versus handling it in PHP memory.

Our network