SCook Posted June 20, 2007 Share Posted June 20, 2007 Here is my question: I am pulling a list of cities from a database. The cities are coming from two different tables and I am putting them into arrays. Now, I need to compare the two arrays and remove any duplicate cities and then end up with one array filled with unique values. I've tried a few things, but with little success. I can just see the answer but not quite yet. Any tips would be great. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/56364-comparing-2-arrays-and-pulling-unique-values-out/ Share on other sites More sharing options...
aniesh82 Posted June 20, 2007 Share Posted June 20, 2007 It is better to change your sql query for retrieving the cities like Distinct(citiesNames) Quote Link to comment https://forums.phpfreaks.com/topic/56364-comparing-2-arrays-and-pulling-unique-values-out/#findComment-278443 Share on other sites More sharing options...
aniesh82 Posted June 20, 2007 Share Posted June 20, 2007 Need to find the result from the array, use array_diff() $result = array_diff($array1, $array2); $unique = array_merge($array2, $result); print_r($unique); Quote Link to comment https://forums.phpfreaks.com/topic/56364-comparing-2-arrays-and-pulling-unique-values-out/#findComment-278445 Share on other sites More sharing options...
birmana Posted June 20, 2007 Share Posted June 20, 2007 hi, i think first u need to merge the two arrays by using array_merge() and then get the unique values by using array_unique(); $both = array_merge($t1, $t2); $t3 = array_unique($both ); hope that works Quote Link to comment https://forums.phpfreaks.com/topic/56364-comparing-2-arrays-and-pulling-unique-values-out/#findComment-278449 Share on other sites More sharing options...
aniesh82 Posted June 20, 2007 Share Posted June 20, 2007 Your suggestion looks good. Quote Link to comment https://forums.phpfreaks.com/topic/56364-comparing-2-arrays-and-pulling-unique-values-out/#findComment-278455 Share on other sites More sharing options...
SCook Posted June 20, 2007 Author Share Posted June 20, 2007 Well, I found a solution before anynoen posted, though i appreciate it. And these will probably work too. What I did was use array_filter() This function itereates through an array and sends each value to a callback function. So, I iterate through the second array and compare each value to every value in the first. If it matches, I return false, which does NOT add that value to the new array calling array_filter. When true, that value from the callback function is added. Then I simply array_merge the two arrays. Too bad PHP doesn't have a built in array compare function, but these method work too, I guess. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/56364-comparing-2-arrays-and-pulling-unique-values-out/#findComment-278513 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.