比较在PHP中两个数组的值
您好我想比较2阵列的所有值,并用真或假的结束。我使用下面的code和会的想到结果会是假的。但事实并非如此,当最后一行运行我希望显示类似
Hi i want to compare all the values of 2 arrays and end up with a true or false . I am using the code below and would of thought that the result would be false . but that is not the case , when the last line runs I would expect a display something like
阵列([0] => 0)
Array ( [0] => 0 )
但我没有得到任何显示,以便认为PHP是高兴,没有什么区别
but I get no display so assume that php is happy that there is no difference
我的code是
$before = array('1', '1', '0', '0', '1', '0' ) ;
$after = array('0', '1', '0', '0', '1', '0' ) ;
$new_array= array_diff($before,$after);
print_r ($new_array) ;
想必和array_diff应该在这里发现了一个区别?任何帮助将是巨大的感谢
surely the array_diff should spot a difference here ? any help would be great thanks
和array_diff
给出了元素在 $之前
而不是 $后
。由于这两种阵列包括 0
和 1
,它返回一个空数组。
array_diff
gives which elements are in $before
but not $after
. Since both arrays consist of '0'
and '1'
, it returns an empty array.
什么你要找的是 和array_diff_assoc
,它看起来在键值在一起。
What you're looking for is array_diff_assoc
, which looks at keys and values together.
待办事项,输出你不会是阵列([0] = 0)
,而阵列([0] =方式> 1)
,因为它给从没有在另一个存在的第一个阵列中的元素
Do note, the output you get will not be Array( [0] => 0 )
, but rather Array( [0] => 1 )
, as it gives the elements from the first array that doesn't exist in the other one.
如果你希望其他的输出,你需要做的和array_diff_assoc($后,前$)
。
If you wish the other output, you'll need to do array_diff_assoc($after, $before)
.