PHP array_unique()Functions

PHP Array ReferenceComplete PHP Array Reference Manual

Examples

Remove duplicate values from an array:

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

Run example »

Definition and Usage

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 kept, and the other values are removed.

Note:The retained array will maintain thefirstarray item's key name type.


Syntax

array_unique(array)

Parameter Description
array Required. Specifies the array.
sortingtype Optional. Specifies the sorting type. Possible values:
  • SORT_STRING - Default. Treats each item as a string.
  • SORT_REGULAR - Arranges each item in regular order (Standard ASCII, without changing the type).
  • SORT_NUMERIC - Treats each item as a number.
  • SORT_LOCALE_STRING - Treats each item as a string, based on the current locale setting (can be changed viasetlocale()).

Technical Details

Return value: Returns the filtered array.
PHP version: 4.0.1+
Changelog: In PHP 5.2.10,sortingtypethe default value was changed back to SORT_STRING.

In PHP 5.2.9,sortingtypethe default value was changed to SORT_REGULAR. In versions before this,sortingtypethe default value was SORT_STRING.


PHP Array ReferenceComplete PHP Array Reference Manual Other extensions