ohdang888 Posted April 13, 2008 Share Posted April 13, 2008 How do i find if 2 values in an array are equal? there could be anywhere from 2 values to, lets say, 50 stored in that array. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/100956-detect-duplicates/ Share on other sites More sharing options...
Orio Posted April 13, 2008 Share Posted April 13, 2008 <?php $arr = array(.......); if(count(array_values($arr)) != count($arr)) echo "Something is duplicated..."; ?> Orio. EDIT- notice I made "==" to "!=" Quote Link to comment https://forums.phpfreaks.com/topic/100956-detect-duplicates/#findComment-516308 Share on other sites More sharing options...
ohdang888 Posted April 13, 2008 Author Share Posted April 13, 2008 thanks orio!!!!!! one more question.. how would i go about detecting which 2 or more are the same.... my array is set up like this.. $array[1] $array[2] $array[3] etc. Thanks again, --Joe Quote Link to comment https://forums.phpfreaks.com/topic/100956-detect-duplicates/#findComment-516310 Share on other sites More sharing options...
Orio Posted April 13, 2008 Share Posted April 13, 2008 If you simply want to keep the unique values, use array_unique(). If you want to figure out what values are the same, it's a bit more complicated than using one function (unless someone else can find a simple solution). I'd go on sorting and then running over the array to check for duplicates. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/100956-detect-duplicates/#findComment-516324 Share on other sites More sharing options...
Barand Posted April 13, 2008 Share Posted April 13, 2008 www.php.net/array_count_values Quote Link to comment https://forums.phpfreaks.com/topic/100956-detect-duplicates/#findComment-516342 Share on other sites More sharing options...
laffin Posted April 13, 2008 Share Posted April 13, 2008 If you simply want to keep the unique values, use array_unique(). If you want to figure out what values are the same, it's a bit more complicated than using one function (unless someone else can find a simple solution). I'd go on sorting and then running over the array to check for duplicates. Orio. Actually u did post the link to an example The comments provided by users provide examples for non-unique values (duplicated). Quote Link to comment https://forums.phpfreaks.com/topic/100956-detect-duplicates/#findComment-516354 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.