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.
array array_unique($array , $sortFlags)
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.
- $array: The first parameter of the array_unique function is an array. Duplicate values are removed from the input array.
- $sortFlag: The second parameter of the array_unique function is the sort flag. It 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.
The PHP array_unique Function returns an array after removing all duplicates from it.
For instance, consider the following examples demonstrating the use of PHP array_unique Function.
<?php $testArray = array( 'john' => '10', 'emma' => '20', 'ross' => '30', 'emma' => '20', 'ben' => '40' ); $finalArray = array_unique($testArray); print_r($finalArray); ?>
[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.
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); ?>
 => 1
 => 3
 => 2
 => 4
 => 6
 => 7
 => 8
 => 9
 => 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.
- 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
becausetheir string representations are same.
- It preserves the keys in the array as shown in the examples.
Kindly leave your valuable comments to help me improve this resource.
Vishesh is currently working as a Senior Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.