比较php中两个数组的差异

时间:2022-10-30 12:48:11
Array ( [34] => A [35] => B [36] => B [37] => C ) //This is the Answer

Compares

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

I have tried array_diff_key and array_diff functions but both only returns

我尝试过array_diff_key和array_diff函数,但两者都只返回

Array()

I would like to get the count of the difference, is there a faster way?

我想得到差异的数量,有更快的方法吗?

What did i do wrong? Thank you very much for your help!

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

3 个解决方案

#1


4  

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

#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;

This should work for you.

这应该适合你。

#3


0  

You can use array_diff_assoc().

您可以使用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

?>

Demo: http://codepad.org/Zzilrn9C

#1


4  

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

#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;

This should work for you.

这应该适合你。

#3


0  

You can use array_diff_assoc().

您可以使用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

?>

Demo: http://codepad.org/Zzilrn9C