Remove Duplicates from PHP Array

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

}

  • Digg
  • TwitThis
  • del.icio.us
  • Netvouz
  • description
  • Reddit
  • Furl
  • NewsVine
  • Simpy
  • Slashdot
  • Spurl
  • StumbleUpon
  • YahooMyWeb
  • TailRank
  • Technorati
  • Facebook
  • Google
  • LinkedIn
  • Live
  • MySpace
  • Ping.fm
  • Yahoo! Buzz
  • E-mail this story to a friend!



Home | PHP | Remove Duplicates from PHP Array