phdphd Posted August 24, 2013 Share Posted August 24, 2013 Hi All, I'm wondering whether there is a quick way to go from [countries] => Array ( [0] => Spain [1] => France [2] => France [3] => USA ) [cities] => Array ( [0] => Madrid [1] => Paris [2] => Paris [3] => Boston ) to [countries] => Array ( [0] => Spain [1] => France [3] => USA ) [cities] => Array ( [0] => Madrid [1] => Paris [3] => Boston ) Actually, it is not important that keys are preserved. Thanks a lot ! Link to comment https://forums.phpfreaks.com/topic/281519-removing-duplicates-of-pairs-of-values-based-on-matching-keys/ Share on other sites More sharing options...
Barrikor Posted August 24, 2013 Share Posted August 24, 2013 Use array_unique() ? http://php.net/manual/en/function.array-unique.php Link to comment https://forums.phpfreaks.com/topic/281519-removing-duplicates-of-pairs-of-values-based-on-matching-keys/#findComment-1446568 Share on other sites More sharing options...
phdphd Posted August 26, 2013 Author Share Posted August 26, 2013 Sorry, my starting arrays were too simple. Let assume the following starting arrays instead. In this example, applying array_unique to each array would result in a 3-value array for countries and in a 4-value array for cities. [countries] => Array ( [0] => Spain [1] => France [2] => France [3] => France [4] => USA ) [cities] => Array ( [0] => Madrid [1] => Paris [2] => Paris [3] => Bordeaux [4] => Boston ) Link to comment https://forums.phpfreaks.com/topic/281519-removing-duplicates-of-pairs-of-values-based-on-matching-keys/#findComment-1446801 Share on other sites More sharing options...
Muddy_Funster Posted August 26, 2013 Share Posted August 26, 2013 Sorry, my starting arrays were too simple. Let assume the following starting arrays instead. In this example, applying array_unique to each array would result in a 3-value array for countries and in a 4-value array for cities. [countries] => Array ( [0] => Spain [1] => France [2] => France [3] => France [4] => USA ) [cities] => Array ( [0] => Madrid [1] => Paris [2] => Paris [3] => Bordeaux [4] => Boston ) Which is actualy exactly what you asked for. Link to comment https://forums.phpfreaks.com/topic/281519-removing-duplicates-of-pairs-of-values-based-on-matching-keys/#findComment-1446803 Share on other sites More sharing options...
phdphd Posted August 26, 2013 Author Share Posted August 26, 2013 In the last example, the result I am looking for is : [countries] => Array ( [0] => Spain [1] => France [3] => France [4] => USA ) [cities] => Array ( [0] => Madrid [1] => Paris [3] => Bordeaux [4] => Boston ) In other words, only [2] => France and [2] => Paris are removed because they represent the only pair of country/city values that appears more than once. Link to comment https://forums.phpfreaks.com/topic/281519-removing-duplicates-of-pairs-of-values-based-on-matching-keys/#findComment-1446813 Share on other sites More sharing options...
Muddy_Funster Posted August 26, 2013 Share Posted August 26, 2013 so you don't want to remove duplicate entries from an array, you want to remove duplicate entries from a series of concatenated array strings generated from two completly unconnected array entities. I'll tell you right now - you have designed this wrong. Anyway, here: $array1 = Array('Spain', 'France', 'France', 'France', 'USA'); $array2 = Array('Madrid', 'Paris', 'Paris', 'Bordeaux', 'Boston'); if(count($array1) === count($array2)){ $compArray = array(); $tot = 1; $matches = 0; foreach($array1 as $key =>$val){ $compArray[$key] = $val.$array2[$key]; } $pairedArray = array_unique($compArray); foreach ($compArray as $cK=>$cV){ if(array_key_exists($cK, $pairedArray)){ $tot++; } else{ unset($array1[$cK]); unset($array2[$cK]); $tot++; $matches++; } } echo "Comparison Compete, $matches duplicates found in $tot items."; } Link to comment https://forums.phpfreaks.com/topic/281519-removing-duplicates-of-pairs-of-values-based-on-matching-keys/#findComment-1446819 Share on other sites More sharing options...
phdphd Posted August 26, 2013 Author Share Posted August 26, 2013 Thanks ! Link to comment https://forums.phpfreaks.com/topic/281519-removing-duplicates-of-pairs-of-values-based-on-matching-keys/#findComment-1446834 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.