PHP array_unique() Function

PHP array_unique() Function

The array_unique() function in PHP removes the duplicate values from the specified array.

Syntax

array_unique (array $array [, int $sort_flags = SORT_STRING])

Parameter

array(required)- This array represents the first input array.

sort_flags (optional)- This parameter is used to modify the sorting behavior. The various sorting type flags are as follows:

  • SORT_REGULAR – It compares the items normally (don't change types)
  • SORT_NUMERIC – It compares the items numerically
  • SORT_STRING (Default Value) – It compares the items as strings
  • SORT_LOCALE_STRING – It compares the items as strings, based on the current locale.

Return

This function returns the filtered array by removing the duplicate values for the array.

Example 1

 

Output

Input Array: 
 Array
 (
     [0] => green
     [1] => red
     [2] => green
     [3] => blue
     [4] => red 
 )
 Unique Array: 
 Array
 (
     [0] => green
     [1] => red
     [3] => blue
 ) 

Example 2

"23", "B"=>"45","C"=> "45", "D"=>"21", "E"=>"90");
 echo("Input Array: \n");
 print_r($array);
 //return the unique array by removing the duplicate elements
 $result = array_unique($array,SORT_NUMERIC );//will compare the items numerically 
 echo("\nUnique Array: \n");
 print_r($result);
 ?> 

Output

Input Array: 
 Array
 (
     [A] => 23
     [B] => 45
     [C] => 45
     [D] => 21
     [E] => 90 
 )
 Unique Array: 
 Array
 (
     [A] => 23
     [B] => 45 
     [D] => 21
     [E] => 90
 ) 

Example 3

"1", "B"=>"2","C"=> 1, "D"=>2, "E"=>3);
 echo("Input Array: \n");
 print_r($array);
 //return the unique array by removing the duplicate elements
 $result = array_unique($array,SORT_NUMERIC);//will compare the items as numeric
 echo("\nUnique Array: \n"); 
 //will return 1,2,3 as it will compare the items as numeric
 print_r($result);
 ?> 

Output

Input Array: 
 Array
 (
     [A] => 1
     [B] => 2
     [C] => 1
     [D] => 2
     [E] => 3
 ) 
 Unique Array: 
 Array
 (
     [A] => 1
     [B] => 2
     [E] => 3
 ) 

Example 4

"Apple", "B"=>"Apple","C"=> "Mango", "D"=>"mango", "E"=>"banana");
 echo("Input Array: \n");
 print_r($array);
 //return the unique array by removing the duplicate elements
 $result = array_unique($array,SORT_STRING  );//will compare the items as string
 echo("\nUnique Array: \n");
 print_r($result);
 ?> 

Output

Input Array: 
 Array
 (
     [A] => Apple
     [B] => Apple
     [C] => Mango
     [D] => mango
     [E] => banana
 )
 Unique Array:  
 Array
 (
     [A] => Apple
     [C] => Mango
     [D] => mango
     [E] => banana 
 ) 

Example 5

"@", "B"=>"@","C"=> "$", "D"=>"%", "E"=>"$");
 echo("Input Array: \n");
 print_r($array);
 //return the unique array by removing the duplicate elements
 $result = array_unique($array,SORT_LOCALE_STRING);//will compare the items as string,based on the current locale
 echo("\nUnique Array: \n"); 
 print_r($result);
 ?> 

Output

Array
 (
     [A] => @
     [C] => $
     [D] => %
 )