PHP - 检查两个数组是否相等

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5678959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:11:01  来源:igfitidea点击:

PHP - Check if two arrays are equal

phparrays

提问by markzzz

I'd like to check if two arrays are equal. I mean: same size, same index, same values. How can I do that?

我想检查两个数组是否相等。我的意思是:相同的大小,相同的索引,相同的值。我怎样才能做到这一点?

Using !==as suggested by a user, I expect that the following would print enterif at least one element in the array(s) are different, but in fact it does not.

!==按照用户的建议使用,如果数组中的至少一个元素不同,我希望以下内容会打印enter,但实际上并非如此。

if (($_POST['atlOriginal'] !== $oldAtlPosition) 
    or ($_POST['atl'] !== $aext) 
    or ($_POST['sidesOriginal'] !== $oldSidePosition) 
    or ($_POST['sidesOriginal'] !== $sideext)) {

    echo "enter";
}

回答by Stefan Gehrig

$arraysAreEqual = ($a == $b); // TRUE if $a and $b have the same key/value pairs.
$arraysAreEqual = ($a === $b); // TRUE if $a and $b have the same key/value pairs in the same order and of the same types.

See Array Operators.

请参阅数组运算符

EDIT

编辑

The inequality operator is !=while the non-identity operator is !==to match the equality operator ==and the identity operator ===.

不等运算符是!=,而非恒等运算符是!==匹配相等运算符==和恒等运算符===

回答by lepe

According to this page.

根据这个页面

NOTE:The accepted answer works for associative arrays, but it will not work as expected with indexed arrays (explained below). If you want to compare either of them, then use this solution. Also, this function may not works with multidimensional arrays (due to the nature of array_diff function).

注意:接受的答案适用于关联数组,但它不会按预期使用索引数组(如下所述)。如果要比较它们中的任何一个,请使用此解决方案。此外,此函数可能不适用于多维数组(由于 array_diff 函数的性质)。

Testing two indexed arrays, which elements are in different order, using $a == $bor $a === $bfails, for example:

测试两个索引数组,其中元素的顺序不同,使用$a == $b$a === $b失败,例如:

<?php
    (array("x","y") == array("y","x")) === false;
?>

That is because the above means:

那是因为上面的意思是:

array(0 => "x", 1 => "y")vs. array(0 => "y", 1 => "x").

array(0 => "x", 1 => "y")array(0 => "y", 1 => "x")..

To solve that issue, use:

要解决该问题,请使用:

<?php
function array_equal($a, $b) {
    return (
         is_array($a) 
         && is_array($b) 
         && count($a) == count($b) 
         && array_diff($a, $b) === array_diff($b, $a)
    );
}
?>

Comparing array sizes was added (suggested by super_ton) as it may improve speed.

添加了比较数组大小(由 super_ton 建议),因为它可以提高速度。

回答by Iggi

Try serialize. This will check nested subarrays as well.

尝试序列化。这也将检查嵌套的子数组。

$foo =serialize($array_foo);
$bar =serialize($array_bar);
if ($foo == $bar) echo "Foo and bar are equal";

回答by Samuel Vicent

Short solution that works even with arrays which keys are given in different order:

简短的解决方案甚至适用于以不同顺序给出键的数组:

public static function arrays_are_equal($array1, $array2)
{
    array_multisort($array1);
    array_multisort($array2);
    return ( serialize($array1) === serialize($array2) );
}

回答by Emil Vikstr?m

Compare them as other values:

将它们与其他值进行比较:

if($array_a == $array_b) {
  //they are the same
}

You can read about all array operators here: http://php.net/manual/en/language.operators.array.phpNote for example that ===also checks that the types and order of the elements in the arrays are the same.

您可以在此处阅读有关所有数组运算符的信息:http: //php.net/manual/en/language.operators.array.php注意例如,===它还检查数组中元素的类型和顺序是否相同。

回答by Sodhi saab

!===will not work because it's a syntax error. The correct way is !==(not three "equal to" symbols)

!===不会工作,因为它是一个语法错误。正确的方法是!==(不是三个“等于”符号)

回答by Marcos Fernandez Ramos

if (array_diff($a,$b) == array_diff($b,$a)) {
  // Equals
}

if (array_diff($a,$b) != array_diff($b,$a)) {
  // Not Equals
}

From my pov it's better to use array_diff than array_intersect because with checks of this nature the differences returned commonly are less than the similarities, this way the bool conversion is less memory hungry.

从我的观点来看,使用 array_diff 比使用 array_intersect 更好,因为通过这种性质的检查,返回的差异通常小于相似性,这样 bool 转换对内存的占用更少。

EditNote that this solution is for plain arrays and complements the == and === one posted above that is only valid for dictionaries.

编辑请注意,此解决方案适用于普通数组,并补充上面发布的仅对字典有效的 == 和 === 。

回答by d?lo sürücü

function compareIsEqualArray(array $array1,array $array):bool
{

   return array_diff($array1,$array2)==[];

}

回答by whitebrow

Another method for checking equality regardless of value order works by using http://php.net/manual/en/function.array-intersect.php, like so:

另一种不考虑值顺序检查相等性的方法是使用http://php.net/manual/en/function.array-intersect.php,如下所示:

$array1 = array(2,5,3);
$array2 = array(5,2,3);
if($array1 === array_intersect($array1, $array2) && $array2 === array_intersect($array2, $array1)) {
    echo 'Equal';
} else {
    echo 'Not equal';
}

Here's a version that works also with multidimensional arrays using http://php.net/manual/en/function.array-uintersect.php:

这是一个也适用于使用http://php.net/manual/en/function.array-uintersect.php 的多维数组的版本:

$array1 = array(
    array(5, 2),
    array(3, 6),
    array(2, 9, 4)
);
$array2 = array(
    array(3, 6),
    array(2, 9, 4),
    array(5, 2)
);

if($array1 === array_uintersect($array1, $array2, 'compare') && $array2 === array_uintersect($array2, $array1, 'compare')) {
    echo 'Equal';
} else {
    echo 'Not equal';
}

function compare($v1, $v2) {
    if ($v1===$v2) {
        return 0;
    }
    if ($v1 > $v2) return 1;
    return -1;
}

回答by Scherbius.com

array_diff — Computes the difference of arrays

array_diff — 计算数组的差异

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

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

array array_diff ( array $array1 , array $array2 [, array $... ] )

Compares array1against one or more other arrays and returns the values in array1that are not present in any of the other arrays.

array array_diff ( array $array1 , array $array2 [, array $... ] )

array1与一个或多个其他数组进行比较,并返回array1任何其他数组中不存在的值。