Remove duplicate values from an array:
<?php$a=array("a"=>"red","b"=>"green","c"=>"red");print_r(array_unique($a));?>The array_unique() function is used to remove duplicate values from an array. If two or more array values are the same, only the first value is retained and the other values are removed.
Note: The retained array will retain the key type of the first array item.
array_unique( array )
parameter | describe |
---|---|
array | Required. Specifies an array. |
sortingtype | Optional. Specifies the sorting type. Possible values: SORT_STRING - Default. Treat each item as a string. SORT_REGULAR - Put each item in regular order (Standard ASCII, does not change type). SORT_NUMERIC - treat each item as a number. SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed with setlocale()). |
Return value: | Returns the filtered array. |
---|---|
PHP version: | 4.0.1+ |
Update log: | In PHP 5.2.10, the default value of sortingtype was changed back to SORT_STRING. In PHP 5.2.9, the default value of sortingtype changed to SORT_REGULAR. Prior to this version, the default value of sortingtype was SORT_STRING. |