PHP array_unique() Function

Definition

The array_unique() returns the unique values with duplicate values removed.

Syntax

PHP array_unique() Function has the following syntax.

array array_unique ( array arr )

Parameter

ParameterIs RequiredDescription
arrayRequired.Array to filter
sortingtypeOptional.How to compare the array elements/items.

Possible values for sortingtype:

  • 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

Example

Returns the unique values with duplicate values removed


<?PHP/*from w  ww .j ava2  s .com*/
$toppings2 = array("A", "B", "B", "C","java2s.com");
$toppings2 = array_unique($toppings2);
print_r($toppings2);
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions