Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Array_diff when there are duplicate array values

I have two arrays containing repeating values:

$test1 = Array(
        "blah1",
        "blah1",
        "blah1",
        "blah1",
        "blah2"
    );

    $test2 = Array(
        "blah1",
        "blah1",
        "blah1",
        "blah2"
    );

I am trying to get array difference:

$result = array_diff($test1,$test2);

echo "<pre>";
print_r($result);

I need it to return array with single value blah1, yet it returns empty array instead...

I suspect it has something to do with fact there are duplicate values in both arrays, but not sure how to fix it...

Please help!!

EDIT:

End up writing this function to do the trick:

function subtract_array($array1,$array2){
    foreach ($array2 as $item) {
        $key = array_search($item, $array1);
        unset($array1[$key]);
    }
    return array_values($array1);
}
like image 269
Acidon Avatar asked Feb 09 '16 02:02

Acidon


People also ask

How do you find duplicate numbers in an array if it contains multiple duplicates PHP?

Definition and Usage. The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed.

How remove duplicates from array of objects in PHP?

The array_unique() is a built-in function in PHP and this function removes duplicate values from an array. If there are multiple elements in the array with same values then the first appearing element will be kept and all other occurrences of this element will be removed from the array.

How can I get duplicate values from two arrays in PHP?

Definition and Usage The array_intersect() function compares the values of two (or more) arrays, and returns the matches. This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.

How do I check if two arrays are equal in PHP?

The array_diff() function compares the values of two (or more) arrays, and returns the differences. This function compares the values of two (or more) arrays, and return an array that contains the entries from array1 that are not present in array2 or array3, etc.

How to remove duplicate values from an array in PHP?

We can use the array_unique () function to remove duplicate values from an array. The array_unique () function is a specialized function for removing the duplicate values from an array. The correct syntax to use this function is as follows.

What is the use of array_diff in PHP?

PHP array_diff () Function The array_diff () is a built-in function of PHP, and this function is implemented for calculating or comparing the difference between two or more arrays. The comparison between arrays is made based on the values of the element between one or more array, and then their differences are returned as a new array.

How to find the difference between two arrays in PHP?

The array_diff () is a built-in function of PHP, and this function is implemented for calculating or comparing the difference between two or more arrays. The comparison between arrays is made based on the values of the element between one or more array, and then their differences are returned as a new array.

Is there a double-sided array_diff?

To anybody wanting a double-sided array_diff - mentioned by rudigier at noxx dot at. Remember, array_diff gives you everything in the first array that isn't in the subsequent arrays. $array1=array('blue','red','green'); $array2=array('blue','yellow','green');


2 Answers

array_diff compares the first array to the other array(s) passed as parameter(s) and returns an array, containing all the elements present in the first array that are not present in any other arrays. Since $test1 and $test2 both contain "blah1" and "blah2", and no other values, actually, the expected behavior of array_diff is the one that you have experienced, that is, to return an empty array, since, there is no element in $test1 which is not present in $test2.

Further read. Also, read some theory to understand what you are working with.

like image 105
Lajos Arpad Avatar answered Oct 03 '22 07:10

Lajos Arpad


Spotted a problem with Acidon's own solution. The problem comes from the fact that unset($array[false]) will actually unset $array[0], so there needs to be an explicit check for false (as David Rodrigues pointed out as well.)

function subtract_array($array1,$array2){
    foreach ($array2 as $item) {
        $key = array_search($item, $array1);
        if ( $key !== false ) {
            unset($array1[$key]);
        }
    }
    return array_values($array1);
}

Some examples

subtract_array([1,1,1,2,3],[1,2]);            // [1,1,3]
subtract_array([1,2,3],[4,5,6]);              // [1,2,3]
subtract_array([1,2,1],[1,1,2]);              // []
subtract_array([1,2,3],[]);                   // [1,2,3]
subtract_array([],[1,1]);                     // []
subtract_array(['hi','bye'], ['bye', 'bye']); // ['hi']
like image 43
Jules Colle Avatar answered Oct 03 '22 09:10

Jules Colle