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] => %
 ) 

Related Topics

PHP str_shuffle() Function

PHP str_shuffle() Function The str_shuffle() function in PHP randomly shuffles all the characters of the specified string. Syntax str_shuffle ( string $str ) Parameter str(required)- This parameter represents the input string to shuffle. Return This function returns the shuffled string. Example 1 ...

1 minute read.

PHP nl_langinfo() Function

PHP nl_langinfo() Function The nl_langinfo() function in PHP returns specific local information. This function does not work on Windows platforms. Syntax nl_langinfo ( int $item ) Parameter Item(required)- This parameter specifies the  integer value of the element or the constant name of the...

1 minute read.

PHP Indexed Array

In PHP, arrays are linear data structures that allow to store multiple elements of the same type under a single variable which helps in saving the time and effort of...

3 minutes read.

PHP Super Global Variables

There are various predefine “superglobals” in PHP. These variables can easily accessible in function or class. Following are the superglobals variables: $GLOBALS $_SERVER $_REQUEST $_POST $_GETetc. PHP $GLOBALS The “$GLOBAL” isa keyword that...

2 minutes read.

PHP addcslashes() Function

The addcslashed() function in PHP is used to add quote string with slashes in a C style. It is case-sensitive function.  Syntax addcslashes (str, charlist ) Parameter str(required)- This parameter represents the string to be escaped. charlist(required)-...

1 minute read.

PHP quoted_printable_encode() Function

PHP quoted_printable_encode() Function The quoted_printable_encode() function in PHP converts a 8 bit string to a quoted-printable string. This function is similar to imap_8bit(), except this one does not require the IMAP module to work. Syntax quoted_printable_encode ( string $str ) Parameter str(required)- This parameter represents...

1 minute read.

PHP empty() Function

PHP empty() Function The empty() function in PHP determines whether the specified variable is empty or not. The specified variable is considered empty if it does not exist or if its value is equal to FALSE. This function...

1 minute read.

PHP print_r() Function

PHP print_r() Function The print_r() function in PHP displays the information stored in a variable. Syntax print_r (mixed $expression [, bool $return] ) Parameter expression(required)- This parameter represents the expression to be printed. return(optional)- This parameter is used to store the output of...

2 minutes read.

PHP fprintf() Function

PHP fprintf() Function The fprintf() function in PHP outputs a formatted string. This function operates as printf() but accepts an array of arguments, rather than a variable number of arguments. Syntax vprintf ( string $format , array $args ) Parameter format(required)- This parameter specifies the string and how to...

4 minutes read.

PHP array_values() Function

PHP array_values() Function The array_values () function in PHP returns all the values from the array and indexes the array numerically. Syntax array_values ( array $array ) Parameter array(required)- This array represents the input array. Return This function returns an indexed array of values. Example 1 Output Input Array: Array ...

1 minute read.

PHP compact() function

PHP compact() function The compact() function in PHP is used to create an array containing variables and their values. It returns the output array whose keys are the variable names, and their corresponding values are...

1 minute read.

PHP setlocale() Function

PHP setlocale() Function The setlocale() function in PHP sets locale information(language, monetary, time and, other information specific for a geographical area.) Syntax setlocale ( int $category , array $locale ) Parameter category(required)- This parameter specified a named constant specifying the category of the functions affected...

1 minute read.

PHP array_reduce() Function

PHP array_reduce() Function The array_reduce() function in PHP reduces the array to a single value using by applying a callback function to the elements of the array. Syntax array_reduce ( array $array , callable $callback [, mixed $initial = NULL ] ) Parameter array(required)- This parameter represents the input array. callback (required)- This...

1 minute read.

PHP array_fill() function

PHP array_fill() function The array_fill() function in PHP fills an user-defined array with num entries of the value that of specified values parameter, where the keys start at the start_index parameter. Syntax array_fill ( int $start_index , int $num , mixed $value ) Parameter start_index(required)- This parameter represents the first index of the...

1 minute read.

PHP array_slice() Function

PHP array_slice() Function The array_slice() function in PHP is used to extract a slice of the array. It returns the sequence of elements from the array as specified by the offset and length parameters. Syntax array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = FALSE ]] ) Parameter array(required)- This array represents the...

2 minutes read.

PHP Environment Setup

To install PHP, We have to installed AMP(Apache, MySQL and PHP) software. There are various AMP software available in the market. XAMPP (Cross-Platform, Apache, MySQL, PHP and Perl) for windows. ...

2 minutes read.

PHP array_rand() Function

PHP array_rand() Function The array_rand() function in PHP picks one or more random keys out of an array. Syntax array_rand ( array $array [, int $num = 1 ] ) Parameter array(required)- This parameter represents the input array. num(optional)- This parameter specifies how many entries should be picked....

1 minute read.

PHP array_uintersect() Function

PHP array_uintersect() Function The array_uintersect() function in PHP calculates the intersection of arrays by comparing the data by a callback function. It returns an array containing all the values of the first input array that are...

2 minutes read.

PHP str_repeat() Function

PHP str_repeat() Function The str_repeat() function in PHP repeats a string a specified number of times. Syntax str_repeat ( string $input , int $multiplier ) Parameter string(required)- This parameter signifies the string to be repeated. multiplier(required)- This parameter represents the number of times the string will be...

1 minute read.

PHP doubleval() Function

PHP doubleval() Function The doubleval() function in PHP is used to give the float value of a variable. This function is an alias of floatval(). Syntax doubleval ( mixed $var ) Parameter var(required)- This parameter represents the name of the variable that...

1 minute read.