The array_unique function removes duplicate values from the given array and returns an array that contains only unique values.
If an array contains duplicate values, the array_unique function will keep the first occurrence of the duplicate element, and also, the key type of the first occurrence of that element is preserved in the result.
What is the syntax of the array_unique function in PHP?
array_unique(array, sorttype)
Parameter | Description |
---|---|
array | The array to remove duplicate values from – Required |
sorttype | Optional. Specifies how to compare the array elements/items. SORT_STRING (default) – Compare elements as strings SORT_REGULAR – Compares normally without changing the type SORT_NUMERIC – Compare as numeric values SORT_LOCALE_STRING – Compare as a string according to the current locale |
Example of array_unique function
Example 1. Remove duplicate values from the array.
<?php
$arr=array("a"=>"R","b"=>"G","c"=>"R");
print_r(array_unique($arr));
?>