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

PHP array_diff_key () Function The array_diff_key () Function in PHP compares the keys from array1 against the keys from arrays. It returns an array containing all the entries from array1 whose keys are absent from all other arrays. This function is like array_diff () except that...

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.

PHP is_resource() Function

PHP is_resource() Function The is_resource() function in PHP finds whether a variable is a resource or not. It returns a Boolean value TRUE if the parameter var is resource, else it returns FALSE Syntax is_resource ( mixed $var ) Parameter var(required)- This parameter represents the value to check. Return This function...

1 minute read.

PHP lcfirst() Function

PHP lcfirst() Function The lcfirst() function in PHP converts the first character of a string to lowercase if that character is alphabetic. Syntax lcfirst ( string $str ) Parameter str(required)- This parameter signifies the input string. Return This function returns a string with the...

1 minute read.

PHP next() Function

PHP next() Function The next() function in PHP advances or moves the internal array pointer one place forward and returns the next array value. Syntax next ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns the next array value...

1 minute read.

PHP Do While Loop

PHP do while loop is used to execute the code at least one time because condition is checked after executing the code. Syntax: do{ //code to be executed  }while(condition); Output <?php $n=1;  do{ echo "$n<br/>";  $n++;  }while($n<=5);  ?> Output 1 2 3 4 5 ← Prev Next →...

1 minute read.

PHP quoted_printable_decode() Function

PHP quoted_printable_decode() Function The quote_printable_decode() function of PHP converts a quoted-printable string to an 8 bit string. It works similar to imap_qprint(), except this one does not require the IMAP module to work. Syntax quoted_printable_decode ( string $str ) Parameter str(required)- This parameter represents the...

1 minute read.

PHP usort() Function

PHP usort() Function The usort() function in PHP sorts  array by values using a user-defined comparison function. Syntax usort ( array &$array , callable $value_compare_func ) array(required)- This parameter represents the input array. value_compare_func(required)- This parameter represents a string that describe a callable comparison function. The comparison...

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

PHP chr() Function The chr() function in PHP generates a single byte string from a number and returns a one-character string containing the character as specified by the unsigned integer. Syntax chr(bytevalue);    Parameter bytevalue(required)- This...

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

PHP vfprintf() Function The vfprintf() function in PHP writes a formatted string to a specified output stream. Syntax vfprintf ( resource $handle , string $format , array $args )  Parameter handle(required)- This parameter represents where to write the string. format(required)- This parameter specifies the string and how to format the...

3 minutes read.

PHP Constant

PHP constant is an identifier(name) for a simple value. The value does not change during the script. It starts with a letter or underscore not $ sign. The define()function is...

1 minute read.

PHP strcmp() Function

PHP strcmp() Function The strcmp() function in PHP compares two strings. It is a binary-safe case-sensitive string function. This function is similar to the strncmp() function, with the difference that you can specify the number of characters from each...

1 minute read.

PHP sha1_file() Function

PHP sha1_file() Function The sha1_file() in PHP calculates the sha1 hash of a file and returns the hash (40-character hexadecimal number). Syntax sha1_file ( string $filename [, bool $raw_output] )  Parameter filename(required)- This parameter represents the filename of the file to hash. raw_output(optional)- This...

1 minute read.

PHP str_pad() Function

PHP str_pad() Function The str_pad() function in PHP pads a string to the given length with another string. Syntax str_pad ( string $input , int $pad_length [, string $pad_string  [, int $pad_type ]] ) Parameter input(required)- This parameter signifies the input string. pad_length(required)- This parameter specifies the length of the new string. If this value...

2 minutes read.

PHP var_export() Function

PHP var_export() Function The var_export() function in PHP is used to get the structured information for the specified variable. This function outputs or returns a parsable string representation of a variable. This function is similar to...

1 minute read.

PHP array_search() Function

PHP array_search() Function The array_search() function in PHP searches the array for the specified value. This function returns the first corresponding key if the value is found. Syntax array_search ( mixed $needle , array $haystack [, bool $strict ])  Parameter needle(required)- This parameter represents the value to...

1 minute read.

PHP key() Function

PHP key() Function The key() function in PHP fetches a key from an array and returns the index element of the current array position. Syntax key ( array $array ) Parameter array(required)- This parameter represents the input array. Return This function returns the key of the...

1 minute read.

PHP str_rot13() Function

PHP str_rot13() Function The str_rot13() function in PHP performs the ROT13 encoding on a string and returns the resulting string. The ROT13 encoding is used to shift every letter by 13 places in the alphabet...

1 minute read.