PHP array_unique Function
last modified March 13, 2025
The PHP array_unique
function removes duplicate values from an
array. It's useful for cleaning data and ensuring unique elements.
Basic Definition
The array_unique
function takes an array and returns a new array
without duplicate values. It preserves the keys of the first occurrence.
Syntax: array_unique(array $array, int $flags = SORT_STRING): array
.
The flags parameter controls comparison behavior (SORT_REGULAR, SORT_NUMERIC).
Basic array_unique Example
This shows the simplest usage of removing duplicate values from an array.
<?php $numbers = [1, 2, 2, 3, 4, 4, 5]; $uniqueNumbers = array_unique($numbers); print_r($uniqueNumbers);
Output: Array ( [0] => 1 [1] => 2 [3] => 3 [4] => 4 [6] => 5 )
.
The function removed duplicates while keeping first occurrences and keys.
String Array Example
Remove duplicate strings from an array while preserving case sensitivity.
<?php $fruits = ["apple", "Apple", "banana", "banana", "orange"]; $uniqueFruits = array_unique($fruits); print_r($uniqueFruits);
Output: Array ( [0] => apple [1] => Apple [2] => banana [4] => orange )
.
Note "apple" and "Apple" are treated as different due to case sensitivity.
Using Flags Parameter
Demonstrate how the flags parameter affects comparison behavior.
<?php $values = ["10", 10, "20", 20, "30"]; $uniqueRegular = array_unique($values, SORT_REGULAR); $uniqueString = array_unique($values, SORT_STRING); echo "SORT_REGULAR: "; print_r($uniqueRegular); echo "SORT_STRING: "; print_r($uniqueString);
SORT_REGULAR treats "10" and 10 as equal (type juggling), while SORT_STRING treats them as different. Choose the appropriate flag for your needs.
Associative Array Example
Remove duplicates from an associative array while preserving keys.
<?php $users = [ "john" => "admin", "jane" => "editor", "bob" => "admin", "alice" => "viewer" ]; $uniqueRoles = array_unique($users); print_r($uniqueRoles);
Output: Array ( [john] => admin [jane] => editor [alice] => viewer )
.
The duplicate "admin" role was removed while keeping the first occurrence.
Multidimensional Array Challenge
Show limitations with multidimensional arrays and a workaround solution.
<?php $data = [ ["id" => 1, "name" => "John"], ["id" => 2, "name" => "Jane"], ["id" => 1, "name" => "John"], ]; // array_unique won't work directly on multidimensional arrays $serialized = array_map('serialize', $data); $unique = array_unique($serialized); $result = array_map('unserialize', $unique); print_r($result);
This workaround serializes each element for comparison, then unserializes back. Output shows only unique objects based on all properties.
Best Practices
- Performance: For large arrays, consider sorting first.
- Type Awareness: Be mindful of type comparison behavior.
- Key Preservation: Remember original keys are kept.
- Multidimensional: Use serialization for complex arrays.
Source
PHP array_unique Documentation
This tutorial covered the PHP array_unique
function with practical
examples showing its usage for removing duplicate array values.
Author
List all PHP Array Functions.