PHP:将两个数组与“array_diff”进行比较只返回“Array()”

时间:2013-12-13 08:40:30

标签: php arrays compare array-difference

我有两个阵列(但实际上它们有更多内容):

数组$ erg

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

和Array $ eannummer:

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

我尝试将它们与array_diff进行比较以获取$ erg中的所有数字但不包含在$ eannummer中

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

这只打印出来

Array ()

但我无法找到原因......

提前致谢

2 个答案:

答案 0 :(得分:1)

babl @ wks35:〜$ cat 17.php 它对我有用:

<?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
)

答案 1 :(得分:0)

这可以帮助您

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

array_diff()