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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_unique(array $array, int $flags = SORT_STRING): array

Parameters

ParameterDescription
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

Remove duplicatesTry it yourself
<?php
print_r(array_unique(["php", "sql", "php", "css", "sql"]));

Output

Array
(
    [0] => php
    [1] => sql
    [3] => css
)
Ignore case and renumberTry it yourself
<?php
$tags = ["PHP", "php", "Php"];
print_r(array_values(array_unique(array_map("strtolower", $tags))));

Output

Array
(
    [0] => php
)

Version history

VersionChange
PHP 4.0Added

In other languages

Was this page helpful?