PHP: Comparing two Arrays with "array_diff" only returns "Array ( )"

188 views Asked by At

I have two Arrays (but in reality they have much more content):

Array $erg

Array ( [0] => 4004708326000 [1] => 4004708392555 [2] => 4004708385106)

and Array $eannummer:

Array ( [0] => 4004708326000 [1] => 4004708392555 [2] => 4004708285234)

I tried to compare them with array_diff to get all the Numbers that are in $erg but not in $eannummer

print_r (array_diff($erg, $eannummer));

this only prints out

Array ()

but I can't manage to find out why...

Thanks in advance

2

There are 2 answers

2
BaBL86 On

babl@wks35:~$ cat 17.php It works for me:

<?php
$erg = Array ( 0 => 4004708326000, 1 => 4004708392555, 2 => 4004708385106);
$eannummer = Array ( 0 => 4004708326000, 1 => 4004708392555, 2 => 4004708285234);

print_r($erg);
print_r($eannummer);
print_r(array_diff($eannummer,$erg));
print_r(array_diff($erg,$eannummer));
?>
babl@wks35:~$ php 17.php 
Array
(
    [0] => 4004708326000
    [1] => 4004708392555
    [2] => 4004708385106
)
Array
(
    [0] => 4004708326000
    [1] => 4004708392555
    [2] => 4004708285234
)
Array
(
    [2] => 4004708285234
)
Array
(
    [2] => 4004708385106
)
0
sergio On

This is help you

$resultDiff = array_diff($array2, $array1);

array_diff()