PHP array_unique() Function
Example
Remove duplicate values from an array:
<?php
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
?>
Try it Yourself »
Definition and Usage
The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed.
Note: The returned array will keep the first array item's key type.
Syntax
array_unique(array, sorttype)
Parameter Values
Parameter | Description |
---|---|
array | Required. Specifying an array |
sorttype | Optional. Specifies how to compare the array elements/items. Possible values:
|
Technical Details
Return Value: | Returns the filtered array |
---|---|
PHP Version: | 4.0.1+ |
PHP Changelog: | PHP 7.2: If sorttype is SORT_STRING, this returns a new array and adds the unique elements. PHP 5.2.9: The default value of sorttype was changed to SORT_REGULAR. PHP 5.2.1: The default value of sorttype was changed back to SORT_STRING. |
❮ PHP Array Reference