Home » Php » php – array_diff() with multidimensional arrays

php – array_diff() with multidimensional arrays

Posted by: admin April 23, 2020 Leave a comment

Questions:

Using array_diff(), I can compare and remove similar items, but what if I have the following arrays?

Array1

Array
(
    [0] => Array
        (
            [ITEM] => 1
        )

    [1] => Array
        (
            [ITEM] => 2
        )

    [2] => Array
        (
            [ITEM] => 3
        )
)

Array2

Array
(
    [0] => Array
        (
            [ITEM] => 2
        )

    [1] => Array
        (
            [ITEM] => 3
        )

    [2] => Array
        (
            [ITEM] => 1
        )
    [3] => Array
        (
            [ITEM] => 4
        )
)

I want to filter out the similar items; result should return 4. How can I rearrange my array so that I can use array_diff()?

How to&Answers:

I would probably iterate through the original arrays and make them 1-dimensional… something like

foreach($array1 as $aV){
    $aTmp1[] = $aV['ITEM'];
}

foreach($array2 as $aV){
    $aTmp2[] = $aV['ITEM'];
}

$new_array = array_diff($aTmp1,$aTmp2);

Answer:

You can define a custom comparison function using array_udiff().

function udiffCompare($a, $b)
{
    return $a['ITEM'] - $b['ITEM'];
}

$arrdiff = array_udiff($arr2, $arr1, 'udiffCompare');
print_r($arrdiff);

Output:

Array
(
    [3] => Array
        (
            [ITEM] => 4
        )
)

This uses and preserves the arrays’ existing structure, which I assume you want.

Answer:

Another fun approach with a json_encode trick (can be usefull if you need to “raw” compare some complex values in the first level array) :

// Compare all values by a json_encode
$diff = array_diff(array_map('json_encode', $array1), array_map('json_encode', $array2));

// Json decode the result
$diff = array_map('json_decode', $diff);

Answer:

A couple of solutions using array_filter that are less performant than the array_udiff solution for large arrays, but which are a little more straightforward and more flexible:

$array1 = [
    ['ITEM' => 1],
    ['ITEM' => 2],
    ['ITEM' => 3],
];

$array2 = [
    ['ITEM' => 2],
    ['ITEM' => 3],
    ['ITEM' => 1],
    ['ITEM' => 4],
];

$arrayDiff = array_filter($array2, function ($element) use ($array1) {
    return !in_array($element, $array1);
});

// OR

$arrayDiff = array_filter($array2, function ($array2Element) use ($array1) {
    foreach ($array1 as $array1Element) {
        if ($array1Element['ITEM'] == $array2Element['ITEM']) {
            return false;
        }
    }
    return true;
});

As always with array_filter, note that array_filter preserves the keys of the original array, so if you want $arrayDiff to be zero-indexed, do $arrayDiff = array_values($arrayDiff); after the array_filter call.

Answer:

you can use below code to get difference

$a1 = Array(
    [0] => Array(
            [ITEM] => 1
        )
    [1] => Array(
            [ITEM] => 2
        )
    [2] => Array(
            [ITEM] => 3
        )
    );

$a2 = Array(
    [0] => Array(
            [ITEM] => 2
        )
    [1] => Array(
            [ITEM] => 3
        )

    [2] => Array(
            [ITEM] => 1
        )
    [3] => Array(
            [ITEM] => 4
        ));

array_diff(array_column($a1, 'ITEM'), array_column($a2, 'ITEM'));

Answer:

Having the same problem but my multidimensional array has various keys unlike your “ITEM” consistently in every array.

Solved it with: $result = array_diff_assoc($array2, $array1);

Reference: PHP: array_diff_assoc

Answer:

Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.

        //Enter your code here, enjoy!

$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);