PHP Tutorial - 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
$toppings2 = array("A", "B", "B", "C","java2s.com");
$toppings2 = array_unique($toppings2);
print_r($toppings2);
?>

The code above generates the following result.