0

我试图从 2 个数组中找出差异。但我很难做到这一点。

$test1 = array();
$test2 = array();

    $sizeUniqMembers1 = sizeof($test1);
    $sizeUniqMembers2 = sizeof($test2);

    print_r($test1);
    print_r($sizeUniqMembers1);

我对 test1 的输出:有 50 个元素。

Array ( [47457] => 4 [32089] => 16 [47555] => 1 [38479] => 15 [47641] => 1 [38601] => 3 [38619] => 3 [38493] => 1 [38458] => 1 [47644] => 5 [37545] => 8 [47632] => 3 [47330] => 1 [41088] => 2 [47499] => 4 [47502] => 2 [47617] => 5 [38556] => 5 [47520] => 2 [47611] => 3 [38639] => 1 [47523] => 3 [38582] => 9 [43198] => 1 [38544] => 1 [47483] => 1 [47458] => 2 [47504] => 2 [36165] => 1 [36858] => 8 [38658] => 1 [38436] => 1 [38656] => 2 [47479] => 1 [38611] => 4 [47585] => 2 [47407] => 1 [47332] => 1 [38655] => 1 [47497] => 1 [43199] => 1 [41141] => 1 [47584] => 3 [47472] => 9 [47541] => 2 [47530] => 1 [47582] => 2 [47452] => 4 [38635] => 3 [41107] => 1 )

print_r($test2);
    print_r($sizeUniqMembers2);

我对 test2 的输出:有 89 个元素。

Array ( [47457] => 4 [32089] => 61 [47555] => 5 [38479] => 65 [47641] => 1 [38601] => 7 [38619] => 9 [38493] => 12 [38458] => 3 [47644] => 5 [37545] => 35 [47632] => 8 [47330] => 4 [41088] => 4 [47499] => 6 [47502] => 6 [47617] => 10 [38556] => 10 [47520] => 2 [47611] => 12 [38639] => 5 [47523] => 12 [38582] => 53 [43198] => 3 [38544] => 1 [47483] => 7 [47458] => 12 [47504] => 3 [36165] => 2 [36858] => 20 [38658] => 1 [38436] => 1 [38656] => 5 [47479] => 2 [38611] => 7 [47585] => 2 [47407] => 2 [47332] => 5 [38655] => 12 [47497] => 10 [43199] => 1 [41141] => 2 [47584] => 18 [47472] => 31 [47541] => 8 [47530] => 2 [47582] => 11 [47452] => 7 [38635] => 14 [41107] => 6 [41149] => 6 [38585] => 4 [41100] => 8 [38652] => 3 [37747] => 3 [47331] => 11 [47620] => 5 [47533] => 2 [47448] => 2 [47615] => 2 [384] => 1 [37206] => 4 [43159] => 1 [47460] => 6 [38584] => 1 [37146] => 1 [47521] => 1 [36599] => 1 [47531] => 1 [38462] => 3 [38651] => 7 [38451] => 2 [36333] => 5 [38452] => 1 [47495] => 1 [38554] => 3 [43175] => 6 [38501] => 6 [38576] => 2 [47323] => 1 [38612] => 1 [38489] => 5 [36110] => 1 [47410] => 1 [38437] => 1 [47490] => 1 [37219] => 2 [47540] => 1 [38430] => 1 ) 

这将留下 39 个元素。所以我正在使用array_diff

$new_array = array_diff($test2,$test1); 
    $sizenew_array = sizeof($new_array);

print_r($new_array);

但该声明只给了我 30 个元素,其中一些甚至没有什么不同。为什么会这样

这是我对 new_array 的输出:

Array ( [32089] => 61 [38479] => 65 [38601] => 7 [38493] => 12 [37545] => 35 [47499] => 6 [47502] => 6 [47617] => 10 [38556] => 10 [47611] => 12 [47523] => 12 [38582] => 53 [47483] => 7 [47458] => 12 [36858] => 20 [38611] => 7 [38655] => 12 [47497] => 10 [47584] => 18 [47472] => 31 [47582] => 11 [47452] => 7 [38635] => 14 [41107] => 6 [41149] => 6 [47331] => 11 [47460] => 6 [38651] => 7 [43175] => 6 [38501] => 6 ) 
4

1 回答 1

2

array_diff() 作用于 VALUES,并忽略 KEYS。例如

php > $x = array(10 => 1, 100 => 2);
php > $y = array(3 => 1);
php > var_dump(array_diff($x, $y));
array(1) {
  [100]=>
  int(2)
}

只返回2值,因为2$y 数组中没有。但请注意,这1不是作为差异返回的,因为它存在于两个数组中,即使其对应的键不同。

于 2013-02-11T20:37:36.880 回答