russthebarber Posted November 25, 2011 Share Posted November 25, 2011 How can I check if an array contains two items that are the same? For example ("apples", "bananas", "oranges") returns false But ("apples", "bananas", "bananas") returns true Quote Link to comment https://forums.phpfreaks.com/topic/251807-two-similar-items-in-an-array/ Share on other sites More sharing options...
Pikachu2000 Posted November 25, 2011 Share Posted November 25, 2011 If you just need to know if duplicates exist in the array, you could compare the results of count() to count(array_unique()). If it isn't the same, there are duplicates. Quote Link to comment https://forums.phpfreaks.com/topic/251807-two-similar-items-in-an-array/#findComment-1291221 Share on other sites More sharing options...
AyKay47 Posted November 25, 2011 Share Posted November 25, 2011 what exactly do you want to do to duplicate values? if you want to remove them, whereas only one of the duplicate values will be present, you can use array_unique. Otherwise, it will get a little more in depth. Edit: Pika beat me to it. Quote Link to comment https://forums.phpfreaks.com/topic/251807-two-similar-items-in-an-array/#findComment-1291222 Share on other sites More sharing options...
russthebarber Posted November 25, 2011 Author Share Posted November 25, 2011 Thanks for the replies. I think Pikachu2000 may have the answer....I'll look into that. Basically this array will store what a user inputs, but I want to return an error and warn the user if he inputs the same thing twice. Quote Link to comment https://forums.phpfreaks.com/topic/251807-two-similar-items-in-an-array/#findComment-1291226 Share on other sites More sharing options...
AyKay47 Posted November 25, 2011 Share Posted November 25, 2011 in that case.. you can most likely compare the count of the original array, and the count of the new array after using array_unique. If the 2 counts are equal, then there are no duplicates and you should proceed with the rest of your code, else, return an error. Quote Link to comment https://forums.phpfreaks.com/topic/251807-two-similar-items-in-an-array/#findComment-1291228 Share on other sites More sharing options...
Pikachu2000 Posted November 25, 2011 Share Posted November 25, 2011 In that case, you could always use in_array() when the user attempts to add an element. If in_array() returns TRUE, disallow the action, and alert the user. But really, I guess it depends on how you want the application to flow. Quote Link to comment https://forums.phpfreaks.com/topic/251807-two-similar-items-in-an-array/#findComment-1291230 Share on other sites More sharing options...
russthebarber Posted November 25, 2011 Author Share Posted November 25, 2011 that works perfectly...thanks again for the good advice! Quote Link to comment https://forums.phpfreaks.com/topic/251807-two-similar-items-in-an-array/#findComment-1291238 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.