Compare array in php

1

I have these two arrays that I need to compare to erase the differences.

Array1 which is equivalent to everything in mysql

Array(
    [0] => Array
        (
            [id] => 133
            [que] => Codigo
            [idcosa] => 9
            [1] => 36
            [2] => 40
            [3] => verde
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
        )

    [1] => Array
        (
            [id] => 134
            [que] => Codigo
            [idcosa] => 9
            [1] => 37
            [2] => 37
            [3] => borrar
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
        )

    [2] => Array
        (
            [id] => 135
            [que] => Codigo
            [idcosa] => 9
            [1] => 36
            [2] => 37
            [3] => colao
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
        )

    [3] => Array
        (
            [id] => 136
            [que] => Codigo
            [idcosa] => 9
            [1] => 36
            [2] => 70
            [3] => Gris marengo
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
        )

)

Array2 that is equivalent to what I have of a Cartesian product of a form

Array (
    [0] => Array
        (
            [0] => 133
            [id] => 133
            [1] => 36
            [que] => Codigo
            [2] => 40
            [idcosa] => 9
            [3] => verde
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
        )

    [1] => Array
        (
            [0] => 135
            [id] => 135
            [1] => 36
            [que] => Codigo
            [2] => 37
            [idcosa] => 9
            [3] => colao
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
        )

    [2] => Array
        (
            [0] => 136
            [id] => 136
            [1] => 36
            [que] => Codigo
            [2] => 70
            [idcosa] => 9
            [3] => Gris marengo
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
        )

)

From what I see the Cartesian product puts a new field in the array that is [0] that has the same number as [id]

If we compare the data I would have to give this as a result:

[1] => Array
    (
        [id] => 134
        [que] => Codigo
        [idcosa] => 9
        [1] => 37
        [2] => 37
        [3] => borrar
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => 
        [9] => 
        [10] => 
    ) 

But for some strange reason he does not give it to me and he tells me that the result of the comparison is 0 or a wrong code.

I am comparing this code $Diferencia = array_diff_assoc($array1,$array2); and I have also tried with array_diff to dry, looking at the php web in case there is another comparator I have not seen anything else (well there are several but I think they do not fit me) is there any Another way to do this? or do I have something wrong that I do not know?

The array_diff_assoc launches this as a result:

Array (
        [3] => Array
            (
                [id] => 136
                [que] => Codigo
                [idcosa] => 9
                [1] => 36
                [2] => 70
                [3] => Gris marengo
                [4] => 
                [5] => 
                [6] => 
                [7] => 
                [8] => 
                [9] => 
                [10] => 
            )

    )

But if you look at that id it is both in 1 and 2. Possibly it is silly but it takes me of head and I do not see it, to see if somebody that knows more than I of php sees where I have the conflict :) thousand thanks to all in advance.

    
asked by Killpe 09.01.2017 в 21:53
source

1 answer

2

You could use array_udiff .

So for example:

<?php

    $result = array_udiff($arr1, $arr2, function($a, $b) {

        // Si es igual
        if ($a['id'] == $b['id']) {
            return 0;
        }
        return -1;
    });

    print_r($result);
?>

Result:

Array
(
    [1] => Array
        (
            [id] => 134
            [que] => Codigo
            [idcosa] => 9
            [1] => 37
            [2] => 37
            [3] => borrar
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
        )

)

Demo

    
answered by 10.01.2017 / 00:36
source