How can I remove duplicate values from an array in PHP?
Use array_unique()
for a one-dimensional array. From the PHP manual:
Takes an input array and returns a new array without duplicate values.
Note that keys are preserved. If multiple elements compare equal under the given flags, then the key and value of the first equal element will be retained.
Note that
array_unique()
is not intended to work on multi dimensional arrays.
Example:
$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)
If you want the values re-indexed, in addition, you should apply array_values
.