Jump to content

Recommended Posts

I have 6 variables that i want to check none of them hold the same number so they are all unique to each other.

 

What is the most efficient way to do this? Surely tons of if statements is not the best way to do about this? Is there a function to check all the vars for identical values at all ?

Link to comment
https://forums.phpfreaks.com/topic/123669-solved-whats-the-best-way/
Share on other sites

Ok thank you, would array_unique return TRUE or FALSE when it is unique?

 

Hmm... no, it actually returns you an array of the unique elements. You might be better off with manually parsing your array if you simply want a boolean value. Try something like this:

<?php
function checkUnique($arr)
{
  $tmp = array();
  foreach ($arr as $v)
  {
    if (in_array($tmp))
    {
      return false;
    }
    $tmp[] = $v;
  }
  return true;
}
?>

Couldn't you also get the size of the original array, run an array_unique and compare the sizes?  They should be the same if there are no duplicates.

 

$a = array(1,2,2,3,4);
$b = array_unique($a);

if(sizeof($a) == sizeof($b)) {
    echo 'All values are unique';
} else {
    echo 'There are duplicate value(s)';
}

 

I didn't try this, but I would think it would work.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.