PHP 8 reference · Array Functions
PHP array_unique() Function
Remove duplicate values from an array, keeping the first occurrence of each and its key.
PHP 8 Since 4.0
Syntax
array_unique(array $array, int $flags = SORT_STRING): array
Parameters
| Parameter | Description |
|---|---|
array |
The input array. |
flags optional |
How values are compared: SORT_STRING, SORT_REGULAR, SORT_NUMERIC. Default: SORT_STRING |
Return value
The array without duplicates (original keys kept).
Examples
<?php
print_r(array_unique(["php", "sql", "php", "css", "sql"]));
Output
Array
(
[0] => php
[1] => sql
[3] => css
)<?php
$tags = ["PHP", "php", "Php"];
print_r(array_values(array_unique(array_map("strtolower", $tags))));
Output
Array
(
[0] => php
)Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
In other languages
set()· Python
Related
array_count_values()· Count how many times each value occurs in an array, returning value => count pairs.array_flip()· Swap keys and values, so each value becomes a key. Values must be strings or integers; duplicates keep the last key.array_values()· Return all the values of an array as a list re-indexed from 0. Often used after array_filter() or unset().- PHP Arrays · tutorial
- PHP Array Functions · tutorial