al Posted March 2, 2010 Share Posted March 2, 2010 Hi fellow PHP Coders, I wonder if anybody could help me with function to compare two arrays and display only value differences undependant from Array values order? Php integrated functions array_diff_assoc or array_diff doesn't give me results I'want. If I compare two arrays bellow with array_diff_assoc I don't get empty array but: [1] => kim Array ( [0] => Mike [1] => Kim ) Array ( [0] => Mike [1] => Mike [2] => Kim [3] => Kim ) Quote Link to comment https://forums.phpfreaks.com/topic/193897-quick-compare-two-arrays-function/ Share on other sites More sharing options...
Psycho Posted March 2, 2010 Share Posted March 2, 2010 What result are you wanting? Since there are duplicates I'm not sure as there are different ways this could be interpreted. For exmple, you might be wanting to exlude one item from the second array for each item in the first array (which would result in one Mike and one Kim) or you might be wanting to exclude every matching item from the second array for every item in the first array (which would result in nothing). Plus, when there are results, do you need to preserve the indexes or not? You need to explain what you want better - examples would help. Quote Link to comment https://forums.phpfreaks.com/topic/193897-quick-compare-two-arrays-function/#findComment-1020443 Share on other sites More sharing options...
Psycho Posted March 2, 2010 Share Posted March 2, 2010 I made an assumption that you are wanting to remove one item from array1 for each single items in array2. So, if array 1 has three "Kim" and array 2 has two "Kim" the result would give you one "Kim". If that is correct, this should work for you (array keys are preserved): <?php function array_diff_unique($array1, $array2) { foreach($array2 as $search) { unset($array1[array_search($search, $array1)]); } return $array1; } $array1 = array('Mike', 'Mike', 'Kim', 'Kim'); $array2 = array('Mike', 'Kim'); $result = array_diff_unique($array1, $array2); print_r($result); ?> Ouput: Array ( [1] => Mike [3] => Kim ) Quote Link to comment https://forums.phpfreaks.com/topic/193897-quick-compare-two-arrays-function/#findComment-1020455 Share on other sites More sharing options...
al Posted March 2, 2010 Author Share Posted March 2, 2010 Thank you mjdamato. No I want exclude every matching item from the second or third or etc.. array for every item in the first array (which would result in nothing). I don't need to preserve the indexes. Like fo this two arrays the result should be: Array ( [0] => John ) Array ( [0] => Mike [1] => John ) Array ( [0] => Mike [1] => Mike [2] => Kim [3] => John ) Quote Link to comment https://forums.phpfreaks.com/topic/193897-quick-compare-two-arrays-function/#findComment-1020461 Share on other sites More sharing options...
gamblor01 Posted March 2, 2010 Share Posted March 2, 2010 I'm confused. If you want to "exclude every matching item from the second array" then your result should be Kim and not John. The simplest logic (though not the most efficient) would be to loop through the first array one element at a time and then search the second array for the current element. So if we do this: step 1: Array1[0] is Mike which is found in the second array (at Array2[0]) so we don't add it to our output list step 2: Array1[1] is Mike which is found in the second array (at Array2[0]) so we don't add it to our output list step 3: Array1[2] is Kim which is NOT in the second array so we add it the output list step 4: Array1[3] is John which is found in the second array (at Array2[1]) so we don't add it to our output list I don't understand what your function is supposed to do. But array_diff() should properly do what I described above. Quote Link to comment https://forums.phpfreaks.com/topic/193897-quick-compare-two-arrays-function/#findComment-1020465 Share on other sites More sharing options...
Psycho Posted March 2, 2010 Share Posted March 2, 2010 Assuming the last example was a mistake as gamblor01 suggests, why would array_diff() not work for you? $array1 = array('Mike', 'Mike', 'Kim', 'Kim', 'John'); $array2 = array('Mike', 'Kim'); $result = array_diff($array1, $array2); print_r($result); Ouput: Array ( [4] => John ) Is that not what you want? No need to loop through the values. Quote Link to comment https://forums.phpfreaks.com/topic/193897-quick-compare-two-arrays-function/#findComment-1020471 Share on other sites More sharing options...
al Posted March 2, 2010 Author Share Posted March 2, 2010 I did what I want with your function. Does it make sense or should I do it some other way? // Compare Name Arrays $namediff = array_diff_unique($secondarray,$firstarray); $diffs = array_diff_unique($firstarray,$namediff); Quote Link to comment https://forums.phpfreaks.com/topic/193897-quick-compare-two-arrays-function/#findComment-1020474 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.