Remove Duplicates from PHP Array

Like this blog? Consider exploring one of our sponsored banner ads...

Before you write a quickie function to remove duplicates from a PHP array, check out the PHP array_unique() function. I was about to write a function to de-dupe an array when I remembered that PHP has a builtin function to do just that.

Here is a simple example:

$dirty = array('brad','jeff','bev','john','kirsten','brad');
$clean = array_unique($dirty);

Alternatively, if you just want to know if there are duplicates in an array and not remove them, you could do something like this:

function isUnique($array){ 
 
     return (array_unique($array) != $array); 
 
}

About this entry