THE WORLD'S LARGEST WEB DEVELOPER SITE

PHP array_unique() Function

❮ PHP Array Reference

Example

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 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)

Parameter Description
array Required. Specifying an array
sortingtype Optional. Specifies how to compare the array elements/items. Possible values:
  • SORT_STRING - Default. Compare items as strings
  • SORT_REGULAR - Compare items normally (don't change types)
  • SORT_NUMERIC - Compare items numerically
  • SORT_LOCALE_STRING - Compare items as strings, based on current locale

Technical Details

Return Value: Returns the filtered array
PHP Version: 4.0.1+
Changelog: The default value of sortingtype was changed back to SORT_STRING in PHP 5.2.10.

The default value of sortingtype was changed to SORT_REGULAR in PHP 5.2.9. Prior to this version, the default value was SORT_STRING.

❮ PHP Array Reference