Definition and Usage
- PHP Version
- 4.0.1+
The array_unique() function removes duplicate values from an input array and returns a new array containing only unique values.
When the input array contains multiple identical values, this function returns a new array that keeps the first occurrence of each value and removes all subsequent duplicates.
Features
- The original input array remains unchanged.
- The new array returned by the
array_unique()function preserves the keys from the input array. - In other words, even if duplicate values are removed in the returned array, the original keys from the input array are preserved.
Basic Example
$array = [1, 2, 3, 1, 2, 4];
$unique_array = array_unique($array);
// Returns a new array while keeping the original input array unchanged
print_r($unique_array);
Array
(
[0] => 1
[1] => 2
[2] => 3
[5] => 4
)
Syntax
array_unique(array $array, int $flags = SORT_STRING): array
Parameters
$array |
Required. The input array from which duplicate values are removed. |
|---|---|
$flags |
Optional. A flag that specifies how duplicate elements are compared.
The following flags can be used:
|
Note: SORT_LOCALE_STRING: Compares strings based on the current locale.
This means strings are compared based on the active locale settings. A locale defines sorting and comparison rules based on language, country, and other regional characteristics. For example, certain languages distinguish between uppercase and lowercase letters or handle characters with special symbols differently. These differences are reflected when comparing strings.
Return Values
- If the input array contains multiple identical values, it returns an array that keeps only the first occurrence of each value and removes all remaining duplicates.
- The original keys from the input array are preserved even when duplicate values are removed.
Changelog
| Version | Description |
|---|---|
| 7.2.0 | When $flags is SORT_STRING, versions before PHP 7.2.0 copied the original array and removed duplicate elements without reindexing the array. Starting with PHP 7.2.0, a new array is created by adding only unique values. As a result, the numeric indexes of the resulting array may differ from previous versions. |
array_unique() Processing Behavior from PHP 7.2.0
$a = [0, 0];
$b = array_unique($a, SORT_STRING);
// Add a new element.
$b[] = 1;
print_r($b);
/* --------------------------------------------------
* [Output Before PHP 7.2.0]
*
* Array
* (
* [0] => 0
* [2] => 1 <-- The next numeric index is 2 due to the removed element.
* )
*
* [Description]
* Because duplicate elements were removed from the original array,
* the removed element affected the numeric index sequence, causing the new
* element to be added at index 2.
* -------------------------------------------------- */
/* --------------------------------------------------
* [Output Since PHP 7.2.0]
*
* Array
* (
* [0] => 0
* [1] => 1 <-- The next numeric index is 1 because a new array is created.
* )
*
* [Description]
* Because the behavior was changed to create a new array by adding
* only unique values, the new element is added at index 1.
* -------------------------------------------------- */
Return Values Based on How Duplicate Values Are Compared
Case Sensitivity
The array_unique() function is case-sensitive.
$array = ['a', 'b', 'c', 'A', 'B', 'C'];
$unique_array = array_unique($array);
print_r($unique_array );
/*
Array (
[0] => a
[1] => b
[2] => c
[3] => A
[4] => B
[5] => C
)
*/
Distinguishing between Numbers and Strings
This section explains how $flags, the second parameter of this function, distinguishes between numbers and strings when comparing duplicate elements.
$array = [1, 2, 01, 03, '1', '2', '01', '02'];
/*
* SORT_REGULAR
* array_unique($array, SORT_REGULAR)
*
* [Description]
* Compares elements without changing their data types (applies loose comparison ==).
*
* The integer 1 and the string '1' evaluate as equal values,
* so the string '1' is removed as a duplicate.
*
* The strings '01' and '02' are also compared against numeric values,
* so they are evaluated as equal to the integers 1 and 2, respectively,
* and are removed as duplicates.
*/
$sort_regular = array_unique($array, SORT_REGULAR);
var_dump($sort_regular);
/*
array(3) {
[0]=> int(1)
[1]=> int(2)
[3]=> int(3)
}
*/
/*
* SORT_NUMERIC
* array_unique($array, SORT_NUMERIC)
*
* [Description]
* Compares elements after converting all values to numeric data types.
*
* The strings '01' and '02' are also converted to the numbers 1 and 2,
* so they are removed as duplicates of the existing numbers 1 and 2.
*/
$sort_numeric = array_unique($array, SORT_NUMERIC);
var_dump($sort_numeric);
/*
array(3) {
[0]=> int(1)
[1]=> int(2)
[3]=> int(3)
}
*/
/*
* SORT_STRING
* array_unique($array, SORT_STRING) - Default
*
* [Description]
* Compares elements after converting all values to string data types.
*
* The integer 1 is converted to the string "1" and evaluates as equal
* to the string '1', so it is removed as a duplicate.
*
* On the other hand, the string '01' is compared as "01" as-is,
* so it is treated differently from "1" and preserved.
*/
$sort_string = array_unique($array, SORT_STRING);
var_dump($sort_string);
/*
array(5) {
[0]=> int(1)
[1]=> int(2)
[3]=> int(3)
[6]=> string(2) "01"
[7]=> string(2) "02"
}
*/
Behavior with Associative Arrays
An associative array is an array whose elements are accessed using user-defined keys rather than sequential numeric indexes. These key-value pairs are used to identify elements within the array.
When removing duplicates, the array_unique() function compares values, not keys.
$array = [
'a' => 'green',
'b' => 'green',
'c' => 'orange'
];
$result = array_unique($array);
print_r($result);
/*
Array (
[a] => green
[c] => orange
)
*/
Behavior with Mixed Arrays
A mixed array combines both indexed and associative array elements, containing both numeric indexes and string keys. Such arrays are used in PHP to represent various data structures.
When removing duplicates, the array_unique() function compares values, not keys, for both indexed and associative elements.
$array = [
'a' => 'green',
'red',
'b' => 'green',
'blue',
'red'
];
$result = array_unique($array);
print_r($result);
/*
Array (
[a] => green
[0] => red
[1] => blue
)
*/
References
See Also
- PHP array_diff() Function – Finding Array Differences by Value
- PHP array_diff_assoc() Function – Comparing Array Differences by Key–Value Pairs
- PHP array_intersect_assoc() Function – Finding Common Elements with Another Array Based on Key-Value Pairs
- PHP array_search() Function – Searching for a Value in an Array