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 convert_uudecode() Function

PHP convert_uudecode() Function The convert_uudecode() function in PHP decodes a uuencoded string. Syntax convert_uudecode ( string $data ) Parameter data- This parameter represents the uuencoded data. Return This function returns the decoded data as a string or FALSE on failure. Example 1 <?php // initializing the string $str...

1 minute read.

PHP $ and $$ Variables

PHP $var variable is a normal variable that stores any value. PHP $$var variable is a reference variable that stores the value of$var inside it. Let us take an example. Example 1 <?php $x =...

1 minute read.

PHP Functions

PHP functions are used to reuse piece of code many times.  There are various built-in functions in PHP. A function will be executed by a call to the function. Following are...

3 minutes read.

PHP Comment

PHP commentis a line of code that is not executed. It is often used by developer to understand the code. PHP support single and multiple comments. In other language like C,...

1 minute read.

PHP array_intersect_uassoc() function

The array_intersect_uassoc () function in PHP is used to compute the intersection of arrays with the help of an additional index check, comparing indexes by a callable function. It returns an array containing...

1 minute read.

PHP strip_tags() Function

PHP strip_tags() Function The strip_tags() function in PHP is sued to strip a string from HTML, XML, and PHP tags. It is a binary-safe function. Syntax strip_tags ( string $str [, string $allowable_tags ] ) Parameter str(required)- This parameter specifies the input string. allowable_tags(optional)- This parameter represents...

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 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...

1 minute read.

PHP strtok() Function

PHP strtok() Function The strtok() function in PHP splits a string into smaller strings or tokenize the specified string.  Syntax strtok ( string $str , string $token ) Parameter str(required)- This parameter represents the input string to split into smaller strings or tokens. token(required)- It signifies...

1 minute read.

PHP Tutorial

PHP Tutorial PHP Introduction PHP is a server-side scripting language which is used for web development. It stands for Hypertext Preprocessor. It is faster than another programming language. It supports multiple databases like MySQL, SQL, PostgreSQL and...

3 minutes read.

PHP is_scalar() Function

PHP is_scalar() Function The is_scalar() function in PHP finds whether a variable is a scalar or not. It returns a Boolean value TRUE if the parameter var is scalar, else it returns FALSE. Scalar variables are those containing an integer, float, string or boolean. This...

1 minute read.

PHP sha1() Function

PHP sha1() Function The sha1() in PHP calculates the SHA-1 hash of a string. Syntax sha1 ( string $str [, bool $raw_output = FALSE ] )  Parameter str (required)- This parameter represents the input string. raw_output(optional)- This parameter takes Boolean value and returns the digest in raw binary format...

1 minute read.

PHP strval() function

PHP strval() function The strval() function in PHP is used to get the string value for the specified variable. This function cannot be used on arrays or objects, if applied then would return only the...

1 minute read.

PHP rsort() Function

PHP rsort() Function The rsort () function in PHP is used to sort an array in reverse order i.e., highest to lowest. Syntax rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to...

1 minute read.

PHP is_object() Function

The is_ object() function in PHP finds whether the specified variable is an object or not. It returns a Boolean value TRUE if the specified var is an object else it returns FALSE. Syntax is_object(mixed$var) var(required)- This parameter represents the...

1 minute read.

PHP array_diff_ukey() Function

The array_diff_ukey() function in PHP compares the keys from array1 against the keys from array2 and returns the difference containing all the entries from array1 that are not present in any of the other arrays. Syntax array_diff_ukey ( array $array1 , array $array2 [, array $... ], callable $key_compare_func ) Parameter array1(required)- This parameter signifies the...

1 minute read.

PHP end() function

PHP end() function The end() function in PHP sets the internal pointer of an array to its last element. This function returns the value of the last element Syntax end ( array &$array )   Parameter array (required)– This parameter represents the input array. Return This function returns...

1 minute read.

PHP trim() Function

PHP trim() Function The trim() Function in PHP strips the whitespace or other characters from the beginning and the end of the given string. The trim() function strips the following characters: " " - an ordinary space."\t"- a tab."\n"- a new...

1 minute read.

PHP strcspn() Function

PHP strcspn() Function The strcspn() function in PHP returns the number of characters (including whitespaces) found in a string before any part of the specified characters is found. It is a binary-safe function. Syntax strcspn ( string $subject , string $mask [, int $start [, int $length ]] ) Parameter subject(required)- This...

1 minute read.

Python Set pop() method

Python Set pop() method The set.pop() method in Python removes an arbitrary element (random item) from the set and returns the element removed. Syntax set.pop()  Parameter NA Return This method returns an arbitrary (random) element from the set. Example 1 #...

1 minute read.