比较数组不打印差异 [英] Comparing arrays not printing the differences
问题描述
这是我的测试代码:
$a = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"21");
$b = array("Peter"=>"35", "Ben"=>"21", "Joe"=>"43");
function leo_array_diff($a, $b) {
$map = array();
foreach($a as $val) $map[$val] = 1;
foreach($b as $val) unset($map[$val]);
return array_keys($map);
}
print_r(leo_array_diff($a, $b));
echo "<br>";
print_r(array_diff($a, $b));
这是它的打印输出:
Array ( [0] => 37 )
Array ( [Ben] => 37 )
我将指的是leo_array_diff()
函数:
i'm going to be referring to the leo_array_diff()
function:
第一个问题:
如您所见,peter是相同的数字,所以很好. $a
ben和$b
ben不同. $a
和$b
joe不同.但这只是表明本是不同的.
as you can see, peter is the same number so thats good. $a
ben and $b
ben is different. $a
and $b
joe is different. but it is only showing ben is different.
也许是因为$a
joe 21与$b
ben是21吗?我该如何改变呢?彼得需要与彼得对应,本需要与本对应,等等...
maybe because $a
joe 21 is same as $b
ben is 21? how can i change that? peter needs to correspond with peter, ben needs to correspond with ben, etc...
第二个问题:
本是不同的,是的,因为37和21在print_r
中只显示Array ( [0] => 37 )
.如何显示Array ( [0] => 21 )
?调用函数时如何编辑函数而不交换参数?
Ben is different, yes, because 37 and 21 but in the print_r
, it only shows Array ( [0] => 37 )
. how can i make it show Array ( [0] => 21 )
? How do I edit the function and not swapping the parameter when calling the function?