array_unique()
function removes duplicate values in the array and returns the result array.
When the values of several array elements are equal, only the first element is retained and the other elements are deleted.
The returned array has the key name.
Note: The preserved array will retain the keyname type of the first array item.
Remove duplicate values from the array:
<?php $a = array ( "a" => "red" , "b" => "green" , "c" => "red" ) ; print_r ( array_unique ( $a ) ) ; ?>
Try it yourself
array_unique ( array )
parameter | describe |
---|---|
array | Required. Specify array. |
sortingtype |
Optional. Specifies how to compare array elements/items. Possible values:
|
array_unique()
first sorts the values as strings, then retains only the first encountered key name for each value, and then ignores all subsequent key names. This does not mean that the first occurrence of the key name of the same value in an unsorted array will be preserved.