Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP array_unique() Function

PHP array_unique() Function

PHP Array ReferenceComplete PHP Array Reference Manual

Example

Remove duplicate values from an array:

"red","b"=>"green","c"=>"red"); print_r(array_unique($a)); ?>

Run Example »


Definition and Usage

The array_unique() function removes duplicate values from an array. If two or more array values are equal, only the first value is kept and the others are removed.

Note:The retained array will keep the key type of thefirstarray item.


Syntax

array_unique(

array

)

Parameters Description
array Required. Specifies the array.
sortingtype Optional. Specifies the sorting type. Possible values:
  • SORT_STRING - Default. Treat each item as a string.
  • SORT_REGULAR - Compare items normally (Standard ASCII, do not change types).
  • 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 by setlocale() ).
  • |

    Technical Details

    Return Value: Returns the filtered array.
    PHP Version: 4.0.1+
    Changelog: In PHP 5.2.10, the default value ofsortingtype was changed back to SORT_STRING.
     在 PHP 5.2.9 中,*sortingtype* 的默认值改为 SORT_REGULAR。在这之前的版本,*sortingtype* 的默认值为 SORT_STRING。 |
    

    PHP Array ReferenceComplete PHP Array Reference Manual