HOWTO · PHP

How to Remove Duplicate Values From an Array in PHP

This article introduces how to remove duplicate values from an array in PHP. It includes array_unique() function.

On this page

To remove repeated values from a PHP array, call array_unique() with the array. It returns an array containing the first value from each group considered equal. The result keeps the keys attached to those first values, so removing duplicates does not necessarily produce consecutive numeric indexes.

Remove duplicate values with array_unique()

The function accepts an array and an optional comparison flag. The basic form is array_unique($array). For example, if a list contains "red", "blue", "red", the returned list contains the first "red" and "blue". The original array is not sorted, and the operation does not change the order of the retained values. Assign the returned array if you want to continue using the deduplicated version:

$colors = ["red", "blue", "red", "green", "blue"];
$uniqueColors = array_unique($colors);

Understand which value and key are kept

When values compare equal under the selected flag, array_unique() retains the first value it encounters and discards later matching values. With an indexed array, a removed item leaves a gap in the keys. With an associative array, the original key for each retained value remains attached to it. This is useful when keys identify records or names, but it can surprise code that assumes the result has indexes 0, 1, 2, ....

The function removes duplicate values; it does not sort by value or key. If output order matters, remember that the retained values stay in their original relative order. Choose a comparison flag based on how the values should be treated as equal, not on a desired display order.

Choose a comparison flag

The optional $flags argument selects the comparison mode. The default, SORT_STRING, compares values as strings. SORT_REGULAR uses PHP’s usual comparison, SORT_NUMERIC compares values numerically, and SORT_LOCALE_STRING compares strings according to the current locale. A flag changes which values count as duplicates; it does not sort the returned array. Mixed types can therefore deduplicate differently under different flags. Use a mode that matches the data’s meaning, and avoid assuming that a numeric comparison will reorder values.

Reindex the result when needed

If downstream code needs a list with consecutive numeric keys, pass the deduplicated result to array_values(). This makes a new numerically indexed array while preserving the retained values’ order. Keep the keys when they carry meaning, such as product IDs or labels; reindex only when consumers expect a plain list. Reindexing is a separate step and is not performed automatically by array_unique().

Verify the result with a PHP example

Run this complete example to inspect the retained values and the effect of reindexing:

$items = ["first" => "pear", "second" => "plum", "third" => "pear", "fourth" => "lime"];
$uniqueItems = array_unique($items);
$listItems = array_values($uniqueItems);

echo "Unique values with original keys:\n";
print_r($uniqueItems);
echo "Same values with consecutive keys:\n";
print_r($listItems);

The first result retains the keys first, second, and fourth; the later pear entry is removed. The reindexed result contains those same values in the same order under consecutive numeric keys. This distinction helps when deciding whether to keep record keys or prepare a list for code that expects ordinary numeric indexes.