array_unique()
array_unique() — Removes duplicate values from an array.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Takes an input array and returns a new array without duplicate values.
Signature
array_unique(array $array, int $flags = SORT_STRING): array
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $array | array | The input array. |
| $flags | int | The optional second parameter flags may be used to modify the comparison behavior using these values: |
Return value
Returns the filtered array.
Example
A runnable snippet:
$input = ["a" => "green", "red", "b" => "green", "blue", "red"]; $result = array_unique($input); print_r($result);
Output:
Array ( [a] => green [0] => red [1] => blue )
Changelog
| Version | Change |
|---|---|
| 7.2.0 | If flags is SORT_STRING, the returned array is built by adding the unique elements to a new array; formerly, it was a copy of array from which the non-unique elements were removed. Both hold the same elements under the same keys, but if the element with the largest integer key is among those ... |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page array_unique() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.