When writing PHP applications, you may need to remove duplicate values while preserving the first occurrence. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.
The array_unique() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.
What Is array_unique() in PHP?
Remove duplicate values while preserving the first occurrence.
Its syntax is:
array_unique(array $array, int $flags = SORT_STRING): array
Parameters used by this operation:
$array— The collection being examined or updated.$flags— An optional flag that controls comparison or processing behavior.
Keys from surviving elements are preserved, so call array_values() for a zero-based list. This is not a recursive deduplicator.
Example 1: Basic Usage
Start with a small, self-contained input so you can inspect exactly how the operation behaves.
<?php
$emails = ["[email protected]", "[email protected]", "[email protected]"];
print_r(array_unique($emails));
Output:
Array
(
[0] => [email protected]
[1] => [email protected]
)
The output shows the result of the first call. Check the printed keys and values as well as their order.
Example 2: Another Important Case
This variation highlights another input shape, a comparison option, or a change in the collection.
<?php
$numbers = [3, 1, 3, 2];
print_r(array_values(array_unique($numbers, SORT_NUMERIC)));
Output:
Array
(
[0] => 3
[1] => 1
[2] => 2
)
Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.
Practical Example: Deduplicate normalized newsletter recipients before sending email
Deduplicate normalized newsletter recipients before sending email. Save the code in a PHP file and adapt the sample data to your project.
<?php
$subscriberEmails = ["[email protected]", "[email protected]", "[email protected]"];
$uniqueEmails = array_values(array_unique($subscriberEmails));
echo "Recipients: " . count($uniqueEmails) . PHP_EOL;
echo implode(", ", $uniqueEmails);
Output:
Recipients: 2
[email protected], [email protected]
The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.
Important Behavior and Edge Cases
Keys from surviving elements are preserved, so call array_values() for a zero-based list. This is not a recursive deduplicator.
- Test the operation with empty or missing input and decide how your application should respond.
- Read the return value independently from any changes to variables passed by reference.
- Validate untrusted input before using the result for permissions, database changes, or user-visible output.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
array_unique() | Remove duplicate values while preserving the first occurrence |
| Related functions | array_count_values() counts occurrences; array_values() reindexes the result. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Assuming input and result have identical keys | Check whether the function reindexes keys or retains original associations. |
| Confusing mutation with a returned copy | Keys from surviving elements are preserved, so call array_values() for a zero-based list. This is not a recursive deduplicator. |
| Choosing a related function with different behavior | array_count_values() counts occurrences; array_values() reindexes the result. |
Key Takeaways
array_unique()helps you remove duplicate values while preserving the first occurrence.- Keys from surviving elements are preserved, so call array_values() for a zero-based list. This is not a recursive deduplicator.
- array_count_values() counts occurrences; array_values() reindexes the result.
- Use the practical example as a starting point, but validate real application inputs.
array_unique() is useful when you need to remove duplicate values while preserving the first occurrence. Use its documented rules and the examples above to choose the right behavior for your data.