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));
?>
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:
  • 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+
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
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.