Compare two multidimensional arrays with array_udiff

0

I have the following code portion to buy two fixes:

$a = array(
         array('cuenta' => 1000), 
         array('cuenta' => 2000)
        );

$b = array(
        array('cuenta' => 1000),
        array('cuenta' => 2000),
        array('cuenta' => 3000)
    );

print_r($a);
print_r($b);

$d = array_udiff($a, $b, function($x, $y) use ($a, $b){
    if($x['cuenta'] == $y['cuenta']){
        return 0;
    }else{
        return 1;
    }
});

print_r($d);

When you ran the above shows me on the screen

// Arreglo $a
Array
(
    [0] => Array
        (
            [cuenta] => 1000
        )

    [1] => Array
        (
            [cuenta] => 2000
        )

)

// Arreglo $b
Array
(
    [0] => Array
        (
            [cuenta] => 1000
        )

    [1] => Array
        (
            [cuenta] => 2000
        )

    [2] => Array
        (
            [cuenta] => 3000
        )

)

// Arreglo $d
Array
(
)

What should I correct in my code or what should I do so that when I executed this code the result of the $d fix is this:

Array
(
    [0] => Array
        (
            [cuenta] => 3000
        )
)
    
asked by Ray 06.01.2018 в 17:40
source

1 answer

1

I made two corrections to your code. 1) I inverted the names of your array, because the second one is more complete. 2) in the condition of different return the value -1

$b = array(
     array('cuenta' => 1000), 
     array('cuenta' => 2000)
    );

$a = array(
    array('cuenta' => 1000),
    array('cuenta' => 2000),
    array('cuenta' => 3000)
);

print_r($a);
print_r($b);

$d = array_udiff($a, $b, function($x, $y) use ($a, $b){
if($x['cuenta'] == $y['cuenta']){
    return 0;
}else{
    return -1;
}
});
echo "<pre>";
print_r($d);
    
answered by 07.01.2018 / 02:22
source