Search code examples
phparraysmultidimensional-arrayfilterarray-difference

Filter rows of a 2d array by the rows of another 2d array


I need to get the difference between these two arrays, I've tried array_diff($array1,$array2) without success, any idea?

array1

Array
(
    [0] => Array
        (
            [status] => 61192106047320064
        )

    [1] => Array
        (
            [status] => 61185038284357632
        )

    [2] => Array
        (
            [status] => 61182890951720960
        )

)

array2

Array
(
    [0] => Array
        (
            [status] => 61185038284357632
        )

    [1] => Array
        (
            [status] => 61182890951720960
        )

)

Solution

  • Maybe I'm misunderstanding, but can't you just do something like this for your specific problem?

    $newStatuses = array();
    
    foreach($array1 as $element1) {
        foreach($array2 as $element2) {
            if($element1['status'] == $element2['status']) {
                continue 2;
            }
        }
    
        $newStatuses[] = $element1;
    }
    

    Each element of $newStatuses will be an array with a 'status' element from array1 that was not in array2.

    So, $newStatuses would be this:

    Array
    (
        [0] => Array
            (
                [status] => 61192106047320064
            )
    
    )