比较在PHP中两个数组的值 [英] Compare to values of two arrays in PHP

查看:249
本文介绍了比较在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).

这篇关于比较在PHP中两个数组的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆