Skip to content
PHP Logo

What are alternative methods to array_unique for removing duplicate values in PHP arrays?

When dealing with PHP arrays, the array_unique function is commonly used to remove duplicate values. However, there are alternative methods to achieve the same result. One such method is to use the array_flip function to flip the keys and values, then use the array_keys function to extract the unique values.

php
// Original array with duplicate values
$array = [1, 2, 2, 3, 4, 4, 5];

// Using array_flip and array_keys to remove duplicates
$uniqueArray = array_keys(array_flip($array));

// Output the unique values
print_r($uniqueArray);