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);
}
}