Jump to content

Quick Compare two Arrays Function


al

Recommended Posts

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
)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.