PHP array_unique function

Last Updated: February 15, 2022

array_unique — Remove duplicate elements from the array

Syntax

 array_unique(array $array, int $flags = SORT_STRING): array

Supports (PHP 4 , PHP 5, PHP 7, PHP 8)

Parameters

Parameter Description
array target array
flags

The optional second parameter flags may be used to modify the sorting behavior using these values:

Sorting type flags:

  • SORT_REGULAR – compare items normally (don’t change types)
  • SORT_NUMERIC – compare items numerically
  • SORT_STRING – compare items as strings
  • SORT_LOCALE_STRING – compare items as strings, based on the current locale.

Return Value

returns the array without duplicates

Example

$input = array("a", "b", "c", "c", "b");

$output = array_unique($input);  
print_r($output);    

Output

Array ( [0] => a [1] => b [2] => c ) 
Test