比较php中两个数组的差异

时间:2011-11-25 10:05:38

标签: php arrays compare

Array ( [34] => A [35] => B [36] => B [37] => C ) //This is the Answer

比较

Array ( [34] => B [35] => C [36] => A [37] => D ) //This is the right data

我尝试了array_diff_keyarray_diff功能,但两者都只返回

Array()

我想得到差异的数量,是否有更快的方式?

我做错了什么?非常感谢你的帮助!

3 个答案:

答案 0 :(得分:4)

count(array_diff_assoc($array1, $array2));

答案 1 :(得分:0)

您可以使用array_diff_assoc()

<?php

$array1 = array('34' => 'A', '35' => 'B', '36' => 'B', '37' => 'C');
$array2 = array('34' => 'B', '35' => 'C', '36' => 'A', '37' => 'D');
$count = count(array_diff_assoc($array1, $array2));

echo $count; //4

?>

演示:http://codepad.org/Zzilrn9C

答案 2 :(得分:0)

$array1 = array ( [34] => A [35] => B [36] => B [37] => C ) 
$array2 = array( [34] => B [35] => C [36] => A [37] => D ) 
$c = count(array_diff($array1, $array2));
echo $c;

这应该适合你。