PHP array_unique() Function
The array_unique() is used to remove duplicate values from an array.
Note: The keys are preserved. array_unique() sorts the values treated as a string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.
Parameter | Description |
---|---|
array | Required. Specifying an array |
sortingtype | Optional. Specifies how to compare the array elements/items. Possible values:SORT_STRING – Default. Compare items as stringsSORT_REGULAR – Compare items normally (don’t change types)SORT_NUMERIC – Compare items numericallySORT_LOCALE_STRING – Compare items as strings, based on current locale |
Syntax
array_unique(array)
Example
$array = array("a"=>"Cooper","b"=>"Samuel","c"=>"Cooper", "d"=>"Cooper");
print_r(array_unique($array));
Output
Array ( [a] => Cooper [b] => Samuel )
Leave a Reply