vampke Posted August 26, 2008 Share Posted August 26, 2008 Hi guys, What is "the best" way to compare the different (integer) values in an array? Is there a function for this available or am i supposed to program it myself with for loops? I found the array unique function which would remove duplicate values. Is it better to do this and check the length of the array? Or is there another and better solution? Quote Link to comment https://forums.phpfreaks.com/topic/121404-solved-compare-values-in-an-array/ Share on other sites More sharing options...
kenrbnsn Posted August 26, 2008 Share Posted August 26, 2008 What are you trying to accomplish? Ken Quote Link to comment https://forums.phpfreaks.com/topic/121404-solved-compare-values-in-an-array/#findComment-626003 Share on other sites More sharing options...
vampke Posted August 26, 2008 Author Share Posted August 26, 2008 I'm trying to check if my users have entered the same value twice on a webform. They enter 5 numbers, but the numbers need to be unique Quote Link to comment https://forums.phpfreaks.com/topic/121404-solved-compare-values-in-an-array/#findComment-626009 Share on other sites More sharing options...
.josh Posted August 26, 2008 Share Posted August 26, 2008 dunno if there's a built in function for that (would like to know myself ) but I think you're wanting something like this? <?php // example array $list = array('a','b','c','d','e'); $c = count($list); for ($x = 0; $x < $c; $x++) { for($y = $x + 1; $y < $c; $y++) { if ($list[$x] == $list[$y]) { $duplicate = true; } // end if $x == $y } // end for $y } // end for $x ?> Quote Link to comment https://forums.phpfreaks.com/topic/121404-solved-compare-values-in-an-array/#findComment-626035 Share on other sites More sharing options...
Fadion Posted August 26, 2008 Share Posted August 26, 2008 As you will have an array of fixed size (5 as u stated) then array_unique() should be alright (again as you stated). <?php $array = array(1,2,3,1,5); $unique = array_unique($array); if(count($unique) == 5){ echo 'alriiight'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/121404-solved-compare-values-in-an-array/#findComment-626049 Share on other sites More sharing options...
vampke Posted August 26, 2008 Author Share Posted August 26, 2008 okay thanks guys, I came up with both solution already, but which one is the best, performance wise, or "any other wise"? Is there a reason to prefer 1 over the other? Quote Link to comment https://forums.phpfreaks.com/topic/121404-solved-compare-values-in-an-array/#findComment-626060 Share on other sites More sharing options...
.josh Posted August 26, 2008 Share Posted August 26, 2008 It depends. Are you wanting to somehow preserve which 2 values are duplicate, as far as error message? Go with mine. If you are simply wanting to display a generic "duplicate items not allowed" message, and your script doesn't need to preserve it? Go with guiltygear's. I would have suggested what he did in the first place, but I guess I just assumed you wanted to somehow preserve it. Quote Link to comment https://forums.phpfreaks.com/topic/121404-solved-compare-values-in-an-array/#findComment-626072 Share on other sites More sharing options...
vampke Posted August 26, 2008 Author Share Posted August 26, 2008 okay thanks, helpfull as ever Quote Link to comment https://forums.phpfreaks.com/topic/121404-solved-compare-values-in-an-array/#findComment-626077 Share on other sites More sharing options...
akitchin Posted August 26, 2008 Share Posted August 26, 2008 if you decide you want to make it a dynamic length (and thus checking against count() becomes less handy), look into using array_count_values(): http://us3.php.net/manual/en/function.array-count-values.php combined with either a sort or a difference (i used difference), you can extract every value that was a duplicate: <?php $original = array(1, 0, 3, 3, 5, 7); $value_counts = array_count_values($original); $duplicates = array_keys(array_diff($value_counts, array(0))); print_r($duplicates); ?> the code's untested, but i think you'll find the difference and intersection functions for arrays is extremely handy for extracting any info you could ever want about an array. Quote Link to comment https://forums.phpfreaks.com/topic/121404-solved-compare-values-in-an-array/#findComment-626079 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.