简体   繁体   English

比较 PHP 中两个数组的值

[英]Compare to values of two arrays in PHP

Hi i want to compare all the values of 2 arrays and end up with a true or false .嗨,我想比较 2 个数组的所有值,并以 true 或 false 结束。 I am using the code below and would of thought that the result would be false .我正在使用下面的代码,并认为结果是 false 。 but that is not the case , when the last line runs I would expect a display something like但事实并非如此,当最后一行运行时,我希望显示类似

Array ( [0] => 0 )数组 ( [0] => 0 )

but I get no display so assume that php is happy that there is no difference但我没有显示所以假设 php 很高兴没有区别

my code is我的代码是

        $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) ;

surely the array_diff should spot a difference here ?肯定 array_diff 应该在这里发现差异吗? any help would be great thanks任何帮助都会非常感谢

array_diff gives which elements are in $before but not $after . array_diff给出了哪些元素在$before但不在$after Since both arrays consist of '0' and '1' , it returns an empty array.由于两个数组均由'0''1' ,因此它返回一个空数组。

What you're looking for isarray_diff_assoc , which looks at keys and values together.您正在寻找的是array_diff_assoc ,它同时查看键和值。

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( [0] => 0 ) ,而是Array( [0] => 1 ) ,因为它给出了第一个数组中不存在的元素.

If you wish the other output, you'll need to do array_diff_assoc($after, $before) .如果您希望其他输出,则需要执行array_diff_assoc($after, $before)

    $before = array('1', '1', '0', '0', '1', '0' ) ;
    $after =  array('0', '1', '0', '0', '1', '0' ) ;

    $new_array= array_diff_assoc($before,$after);

    print_r ($new_array) ;

See http://php.net/manual/en/function.array-diff.phphttp://php.net/manual/en/function.array-diff.php

"Multiple occurrences in $array1 are all treated the same way." “$array1 中的多次出现都以相同的方式处理。”

So, since all you have a zeros and ones, everything is "the same."所以,因为你有一个零和一个,所以一切都是“相同的”。

Yes, array_diff does spot a difference.是的, array_diff确实发现了差异。 It finds the differences between the following arrays with the first one.它找出以下数组与第一个数组之间的差异。 It doesn't compare 0 to 0 and 1 to 1, however.但是,它不会比较 0 到 0 和 1 到 1。 It simply checks if each value in Array1 is in Array2 ... ArrayN.它只是检查 Array1 中的每个值是否在 Array2 ... ArrayN 中。 This function returns an array of all the occurrences in Array1 that were not found in the other arrays, not a true/false boolean.此函数返回一个数组,其中包含 Array1 中在其他数组中未找到的所有事件,而不是 true/false 布尔值。 See example 1 in the documentation.请参阅文档中的示例 1。

Hi i want to compare all the values of 2 arrays and end up with a true or false嗨,我想比较 2 个数组的所有值并以真或假结束

$bool = ($array1 == $array2);

http://us2.php.net/manual/en/language.operators.array.php http://us2.php.net/manual/en/language.operators.array.php

It might sound silly, but comparing two array of different lengths will NOT yield expected difference.这听起来可能很傻,但比较两个不同长度的数组不会产生预期的差异。 Check the length of the arrays first and if they match then use array_diff .首先检查数组的长度,如果它们匹配,使用array_diff Otherwise your diff will always be empty.否则你的差异将永远是空的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM