计算两个平面关联数组之间的差异

问题描述 投票:0回答:3
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_key
array_diff
函数,但都只返回

Array()

我想计算差异的数量,有没有更快的方法?我做错了什么?

php arrays filter associative-array array-difference
3个回答
4
投票
count(array_diff_assoc($array1, $array2));

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;

这应该适合你。


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

© www.soinside.com 2019 - 2024. All rights reserved.