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

The strtolower() function in PHP is used to convert a string to lowercase. This function is binary-safe. The related functions are as follows: strtoupper()lcfirst()ucfirst()ucwords() Syntax strtolower ( string $string ) Parameter string(required)- This parameter represents the input string. Return This function returns a string with all alphabetic...

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

PHP array_splice() Function The array_splice() function in PHP is used to remove a portion of the array and replaces them with the elements of the replacement array. Syntax array_splice (array &$input, int $offset [, int $length = count($input) [, mixed $replacement = array() ]] ) Parameter input(required)- This array represents the...

2 minutes read.

PHP MySQL Database Tutorial

MySQL is the most popular database system that is used within PHP. Let's first understand about MySQL. What is MySQL? MySQL is open-source Relational Database Management System (RDMS). The name of MySQL is...

13 minutes 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 nl2br() Function

PHP nl2br() Function The nl2br() function in PHP inserts HTML line breaks before all newlines in a string. Syntax nl2br ( string $string [, bool $is_xhtml] ) Parameter sting(required)- This parameter specifies the input string. is_xhtml(optional)- This parameter states whether to use XHTML compatible line breaks...

1 minute read.

PHP For loop with Example

For loop in PHP : The for loop is the most potent loop, it combines the abilities to set up variables as we enter the loop, test for conditions while iterating...

3 minutes read.

PHP reset() Function

PHP reset() Function The reset() function in PHP is used to set the internal pointer of an array to its first element and returns the value of the first array element. Syntax reset ( array &$array )  Parameter array(required)- This parameter represents...

1 minute read.

PHP strtr() Function

PHP strtr() Function The strtr() function in PHP translates or replaces certain characters in a string. Syntax strtr ( string $str , string $from , string $to ) or strtr ( string $str , array $replace_pairs ) Parameter str(required)- This parameter specifies the string to translate from(required)- This parameter represents the string being translated to the parameter ‘to’. to(required)- It specifies what...

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

PHP array_replace() Function The array_replace() function in PHP replaces the elements from passed arrays into the first array with values having the same keys in each of the following arrays. Syntax array_replace ( array $array1 [, array $... ] ) Parameter array(required)- This parameter represents...

1 minute read.

PHP Switch Statement with Example

PHP switch statement The PHP switch statement is used to execute one condition from many conditions.It similar to if statements on the same expression. Syntax: switch(n) {             case1: statement if n=case1; break;             case2:             statement if n=case2; break;             case3:            ...

3 minutes read.

PHP array_pad() Function

PHP array_pad() Function The array_pad() function in PHP pads the array to the given length and value and returns a copy of the array padded to size and value specified by size and value parameter. The padding takes place as...

1 minute read.

WordPress Plugin

WordPress Plugin is a process used to add or extend the functionality which can be enabled or disabled, and it does not interfere with the software and its code. WordPress is lightweight and smooth...

2 minutes read.

PHP array_diff() Function

The array_diff () function in PHP compares array1 against other specified arrays and returns the values in array1 that are not present in any of the other arrays. Syntax array_diff(array$array1,array$array2[,array$...] ) Parameter array1(required)- This parameter signifies the input array to...

1 minute read.

PHP Continue

What is continue in PHP? Inside the loop, the continue statement is used to control the loop in preprocessor hypertext. PHP utilizes the continue keyword to implement the continue statement, which...

3 minutes read.

PHP Magic Constants

There are various predefine PHP Magic Constants. It starts with underscore(__) and ends with double underscore.  Magic Constant Description __LINE__ To check current line number of the file. __FILE__ To check full path and filename of...

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

PHP strripos() Function The strripos() function in PHP finds the position of the last occurrence of a substring in the specified string. It is a case-insensitive and binary-safe function. Syntax strripos ( string $haystack , mixed $needle [, int $offset ] ) Parameter haystack(required)- This parameter signifies the...

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.