PHP array_unique Function | Removing Duplicates From Array

Many times we require an array not containing any duplicate values. PHP provides an inbuilt function for the same. The PHP array_unique Function removes duplicates from an array. If there are more than one duplicate value in an array, then the one appearing first is preserved.

Note, that according to the function two values are considered same only if their string representations are same ie. (string) $element1 ==== (string) $element2.

Syntax

array array_unique($array , $sortFlags)

ParametersT

There are two parameters in PHP array_unique Function. Out of the two, one parameter is mandatory while the other one is optional. Let’s take a look at the parameters.

  1. $array: The first parameter of the array_unique function is an array. Duplicate values are removed from the input array.
  2. $sortFlag: The second parameter of the array_unique function is the sort flagIt may be used to modify the sorting behavior of the function. The possible values of the sort flag are as follows:
  • SORT_REGULAR: This is the default value of sort flag. It tells the function to compare items normally (don’t change types).
  • SORT_NUMERIC: This value tells the function to compare items numerically.
  • SORT_STRING: This value tells the function to compare items as strings.
  • SORT_LOCALE_STRING: This value tells the function to compare items as strings, based on the current locale.

Return Value

The PHP array_unique Function returns an array after removing all duplicates from it.

Examples

For instance, consider the following examples demonstrating the use of PHP array_unique Function.

Example 1

<?php        
        $testArray = array(
		'john' => '10',
		'emma' => '20',
		'ross' => '30',
		'emma' => '20',
		'ben' => '40'
	);
	$finalArray = array_unique($testArray);
	print_r($finalArray);
?>
Output     
Array
(
[john] => 10
[emma] => 20
[ross] => 30
[ben] => 40
)

The value [email protected] is repeated twice in the above example. As you can observe, array_unique function preserves the first instance and removes the subsequent ones from the output array. As a result, the output array contains only unique email ids.

Example 2

Let’s consider another example involving numeric values.

<?php
        $testArray = array(1,3,2,4,3,6,4,7,7,4,2,3,6,7,8,2,3,4,6,4,7,8,9,0);
	$finalArray = array_unique($testArray);
	print_r($finalArray);
?>
     Array
(
[0] => 1
[1] => 3
[2] => 2
[3] => 4
[5] => 6
[7] => 7
[14] => 8
[22] => 9
[23] => 0
)

The array contains repeated numeric values which are present in random order. The PHP array_unique Function removes all duplicate values from the array. As a result, the output array contains only unique values.

Important Note

  • The array_unique Function does not work with multidimensional arrays. Hence, use it only with single dimensional arrays.
  • Two elements are considered equal only if their string representations are equal. Therefore, integer 1 and string 1 will match because their string representations are same.
  • It preserves the keys in the array as shown in the examples.

Conclusion

In conclusion, we discussed PHP array_unique Function. You may go through other PHP array functions. Additionally, please you may go through the official documentation of array_unique.

Kindly leave your valuable comments to help me improve this resource.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *