PHP array_unique() Function

Remove duplicate values from an array

Try array_unique() - edit and run
Output
Array
(
    [0] => 3
    [1] => 1
    [3] => 2
)
Array
(
    [0] => 3
    [1] => 1
    [2] => 2
)

What does array_unique() do?

array_unique() returns a copy of the array with duplicate values removed. The first occurrence of each value is kept together with its original key. Values are compared as strings by default.

Syntax

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

Parameters

  • $array - The input array.
  • $flags - How values are compared: SORT_STRING (default), SORT_REGULAR (normal == comparison, needed for nested arrays), SORT_NUMERIC or SORT_LOCALE_STRING.

Return value

The filtered array with original keys preserved. Use array_values() to re-index.

array_unique() examples

Every example below shows the exact output it produces. Paste any of them into the editor above (or the online compiler) to run it.

1. Remove duplicates and reindex

<?php
$numbers = [3, 1, 3, 2, 1];
print_r(array_unique($numbers));
print_r(array_values(array_unique($numbers)));

Output:

Array
(
    [0] => 3
    [1] => 1
    [3] => 2
)
Array
(
    [0] => 3
    [1] => 1
    [2] => 2
)

2. Case-insensitive duplicates and mixed types

<?php
$tags = ["PHP", "php", "Html", "html", "CSS"];
print_r(array_unique($tags));

// Keep the first spelling of each tag, ignoring case
print_r(array_intersect_key($tags, array_unique(array_map('strtolower', $tags))));

// 1, "1", 1.0 and true all become "1" as strings
var_dump(array_unique([1, "1", 1.0, true]));

Output:

Array
(
    [0] => PHP
    [1] => php
    [2] => Html
    [3] => html
    [4] => CSS
)
Array
(
    [0] => PHP
    [2] => Html
    [4] => CSS
)
array(1) {
  [0]=>
  int(1)
}

3. Remove duplicate rows by a field

Re-keying with array_column($rows, null, "email") keeps the last row per email; the array_filter() version keeps the first.

<?php
$rows = [
    ["id" => 1, "email" => "[email protected]"],
    ["id" => 2, "email" => "[email protected]"],
    ["id" => 3, "email" => "[email protected]"],
];
$lastWins = array_values(array_column($rows, null, "email"));
echo implode(", ", array_column($lastWins, "id")) . "\n";

$seen = [];
$firstWins = array_filter($rows, function ($row) use (&$seen) {
    if (isset($seen[$row["email"]])) {
        return false;
    }
    return $seen[$row["email"]] = true;
});
echo implode(", ", array_column($firstWins, "id")) . "\n";

Output:

3, 2
1, 2

Common mistakes and gotchas

  • Nested arrays: with the default SORT_STRING, arrays are converted to the string "Array" and trigger warnings. Use SORT_REGULAR or de-duplicate by a field.
  • Keys are kept: the result can have gaps (0, 1, 3); call array_values() before using it as a list or JSON array.
  • Performance: for large lists of strings or integers, array_keys(array_flip($arr)) is much faster (it keeps the last key instead of the first, but the values are the same).

PHP array_unique() FAQ

Does array_unique() keep the first or last duplicate?

The first. Each value keeps the key of its first occurrence.

How do I remove duplicates case-insensitively?

Run array_unique() on a lower-cased copy and use array_intersect_key() to pick the original values, as in the example above.

How do I use array_unique() on a multidimensional array?

Use array_unique($rows, SORT_REGULAR) for identical rows, or de-duplicate by a field with array_column($rows, null, "field").