EchoFool Posted September 10, 2008 Share Posted September 10, 2008 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/123669-solved-whats-the-best-way/ Share on other sites More sharing options...
obsidian Posted September 10, 2008 Share Posted September 10, 2008 The best way would be to use an array. You can then use methods such as array_unique() on it to see what you get. Quote Link to comment https://forums.phpfreaks.com/topic/123669-solved-whats-the-best-way/#findComment-638615 Share on other sites More sharing options...
EchoFool Posted September 10, 2008 Author Share Posted September 10, 2008 Ok thank you, would array_unique return TRUE or FALSE when it is unique? Quote Link to comment https://forums.phpfreaks.com/topic/123669-solved-whats-the-best-way/#findComment-638616 Share on other sites More sharing options...
pocobueno1388 Posted September 10, 2008 Share Posted September 10, 2008 Ok thank you, would array_unique return TRUE or FALSE when it is unique? http://us3.php.net/array_unique Quote Link to comment https://forums.phpfreaks.com/topic/123669-solved-whats-the-best-way/#findComment-638620 Share on other sites More sharing options...
obsidian Posted September 10, 2008 Share Posted September 10, 2008 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/123669-solved-whats-the-best-way/#findComment-638621 Share on other sites More sharing options...
CroNiX Posted September 10, 2008 Share Posted September 10, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/123669-solved-whats-the-best-way/#findComment-638637 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.