Page 1 of 1

Compare Multiple Strings in Search of Duplicates

Posted: Thu Sep 15, 2005 11:32 am
by cfytable
I have six variable variables. It is OK if multiple variables have 'NULL' values, but, of those with values, I want to make sure that none have duplicate values. In other words, it is OK for both variable1 and variable2 to be equal to NULL, but I don't want variable1 and variable2 to have a value of "Tom". Any ideas on how best to compare the strings based on these requirements?

Posted: Thu Sep 15, 2005 11:53 am
by John Cartwright
put them into an array and array_unique() them

Posted: Thu Sep 15, 2005 12:20 pm
by cfytable
Doesn't array_unique() just reduce the array to the non-duplicate items, as opposed to returning whether duplicates exist or not? Is there a function that does the latter?

Posted: Thu Sep 15, 2005 12:45 pm
by shiznatix
you could do

Code: Select all

foreach ($array as $val)
{
  if (false !== isset($new[$val]))
    $new[$val] = 'set';
  else
    //DUPLICATE AHHHHHH
}
you can change that to $new[$val] = $new[$val]++ to count how many times a option is duplicated too!

Posted: Thu Sep 15, 2005 5:59 pm
by feyd

Code: Select all

if($array == array_unique($array)) { echo 'no duplicates there were, matey.'; } else { echo 'ya have duplicates matey.'; }