Introduction
In this article, we will learn how to remove duplicate values in PHP array. PHP arrays could contain mixed types and duplicate values, and sometimes we need to find duplicate values in an array PHP and remove them. PHP provides a useful function for removing duplicates in arrays.
The function that we are going to use is array_unique
. First, we will explore this function and then use it for removing duplicates. So, let’s get started without any further ado.
PHP array_unqiue
Description
This function removes duplicate values from an array.
Function Signature
array_unique(array $array, int $flags = SORT_STRING): array
Arguments
- array – The input array
- flags – Optional argument that alters the behavior of this function.
Return Type
The filtered array
Example#1 – Remove duplicate values in PHP array
Let’s take a straightforward example and see the working of array_unqiue.
<?php
$arr = ["Red","Yellow","Green","White","Red","Pink","Yellow","Black","White"];
$arr = array_unique($arr);
print_r($arr);
/*
OUTPUT
Array
(
[0] => Red
[1] => Yellow
[2] => Green
[3] => White
[5] => Pink
[7] => Black
)
*/
?>
Simple, isn’t it? The function retains the index. To reindex the array, we could use array_values
as.
$arr = array_values(array_unique($arr));
print_r($arr);
/*
OUTPUT
Array
(
[0] => Red
[1] => Yellow
[2] => Green
[3] => White
[4] => Pink
[5] => Black
)
*/
Awesome. In the following example, we will see an array with integer and string types. We will see how the function works there.
Example#2 – Remove duplicate values in PHP array
Let’s see an array with integer and string values that are similar.
<?php
$arr = [10,"10",9,"9",8,"8","10","8"];
$arr = array_unique($arr);
print_r($arr);
/*
OUTPUT
Array
(
[0] => 10
[2] => 9
[4] => 8
)
*/
?>
The function compares these values as strings even though there are integers as well. This behavior is because of the default flag argument, which is SORT_STRING. There are other options too.
- SORT_NUMERIC
- SORT_REGULAR
- SORT_STRING
- SORT_LOCALE_STRING
Next, we will how to find duplicate values in array PHP that is multidimensional.
Example#3 – Remove duplicate values in PHP array
An important note here is that the array_unique function doesn’t work with multidimensional arrays. However, there’s a way out to the problem using a recursive approach. Here’s a function that uses array_unique recursively.
function removeDuplicates(&$arr) {
//Access the array values by reference.
foreach($arr as $key=>&$value)
{
if(gettype($value) == "array") {
removeDuplicates($value);
$value = array_unique($value);
}
}
}
Let’s use this function to check duplicate values in array PHP and strip them out.
$arr = [
[1,1,[2,2,[3,3,[4,4,[5,5]]]]]
];
removeDuplicates($arr);
print_r($arr);
Here’s the output.
Array
(
[0] => Array
(
[0] => 1
[2] => Array
(
[0] => 2
[2] => Array
(
[0] => 3
[2] => Array
(
[0] => 4
[2] => Array
(
[0] => 5
)
)
)
)
)
)
Voila! A clever fix to a problem. Next, we will see how to remove duplicate objects from a PHP array.
Example#4 – Remove duplicate objects in PHP array
Before we discuss removing duplicate objects, Let’s see the class Person
that has ID and NAME fields.
class Person {
public $id;
public $name;
function __construct($id,$name) {
$this->id = $id;
$this->name = $name;
}
function get_id() {
return $this -> $id;
}
}
Next, here’s an array of Person objects.
$persons = [
new Person("1","Robert"),
new Person("2","Anna"),
new Person("1","Robert"),
new Person("3","Susi"),
new Person("4","Richard"),
new Person("2","Anna"),
];
Some of the person instances are duplicates. Now, if we call array_unqiue just as we did before, we will run into errors. So, the way out is to use the SORT_REGULAR
flag, which doesn’t do type conversion to check duplicate values in array PHP.
$persons_unique = array_unique($persons,SORT_REGULAR);
print_r($persons_unique);
/*
OUTPUT
(
[0] => Person Object
(
[id] => 1
[name] => Robert
)
[2] => Person Object
(
[id] => 2
[name] => Anna
)
[3] => Person Object
(
[id] => 3
[name] => Susi
)
[4] => Person Object
(
[id] => 4
[name] => Richard
)
)
*/
Great!! We have removed duplicate objects. So the array_unqiue is a robust function to strip out duplicates in PHP arrays. We have seen it in different contexts and explored it well in depth.
Conclusion
In this article, we have seen how to find and remove duplicate values in PHP array. We have explored the array_unique function that does a lot behind the scenes and returns an array with unique values. The function takes a third flag argument, and the flag alters the behavior of the function. We have seen the SORT_REGULAR flag for removing duplicate objects from the array. So, we hope you’ve learned the function and its uses in different contexts. That’s all for this article. Stay tuned for more informative articles and tutorials related to PHP.