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:

PHP:
  1. $dirty = array('brad','jeff','bev','john','kirsten','brad');
  2. $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:

PHP:
  1. function isUnique($array){
  2.  
  3.      return (array_unique($array) != $array);
  4.  
  5. }

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Reddit
  • Furl
  • NewsVine
  • Simpy
  • Slashdot
  • Spurl
  • StumbleUpon
  • YahooMyWeb
  • TailRank

Home | PHP | Remove Duplicates from PHP Array