PHP Remove Duplicates from Array Example

Remove duplicate values from an array with array_unique(). Edit the code below and press Run to execute it with real PHP in your browser.

Code Editor
Output
Original: 1, 2, 2, 3, 4, 4, 5
Unique: 1, 2, 3, 4, 5
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

How this remove duplicates from array example works

array_unique() keeps the first occurrence of each value and preserves its key, which leaves gaps in the keys. array_values() re-indexes the result to 0, 1, 2 ...