Thursday, April 4, 2013

Remove duplicate values from an array in PHP

A friend of mine asked me about a small problem: how to remove duplicate values from an array in PHP.

Considering the target was to use recursive function, I gave him the following code:

function removeDuplicates(&$array) {
static $newArray=array();
if(current($array)){
if(@++$newArray[current($array)] > 1) {
unset($array[key($array)]);
} else {
next($array);
}
removeDuplicates($array);
}
}

But still, this is not a very good approach. So i made few other functions and test them.

Using foreach:

function removeDuplicatesForeach(&$array) {
$newArray=array();
foreach($array as $key=>$val){
if(isset($newArray[$val])){
unset($array[$key]);
continue;
}
$newArray[$val] = 1;
}
}
A better approach, slightly faster then the recursive function, but still not fast enough. On a random generated array of 100 values, it still lasted 0.07 millis.

So, i tried using array internal pointers:

function removeDuplicatesWhile(&$array){
$newArray=array();
do{
if(isset($newArray[current($array)])){
unset($array[key($array)]);
continue;
}
$newArray[current($array)] = 1;
} while(next($array) !== false);
}
And the resutl was 0.12 ms. Not faster than  foreach, and this will not work properly if you have a boolean false value.

Then i tried using php functions. Even if there are no php functions to remove duplicates, you can try using array_flip() and array_keys() functions.

function removeDuplicatesNatural(&$array) {
$array = array_keys(array_flip($array));
}
The result: 0.1 ms. Still NOT faster then foreach. I think this is because the value given for array_flip is copied, not taken by reference.

This is just something I do when I get bored. So using foreach is not really faster then array_flip ... this depends on the array you are trying to filter.

I hope this will help someone ...





No comments:

Post a Comment