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

时间:2022-10-24 22:25:00

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 enter if 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";
}

12 个解决方案

#1


362  

$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 ===.

不等式操作符为!=,而非标识操作符为!=,以匹配等式操作符==,标识操作符=== =。

#2


51  

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 == $b or $a === $b fails, 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").

数组(0 = > " x ",1 = > " y ")和数组(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建议),因为它可以提高速度。

#3


29  

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";

#4


9  

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

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

#5


9  

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.php Note 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注意,例如==也检查数组中元素的类型和顺序是否相同。

#6


7  

You can use array_diff_assoc to check for the differences between the two.

您可以使用array_diff_assoc检查两者之间的差异。

#7


2  

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;
}

#8


2  

Working 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) );
}

#9


1  

One way: (implementing 'considered equal' for http://tools.ietf.org/html/rfc6902#section-4.6)

一种方法:(为http://tools.ietf.org/html/rfc6902# sec-4.6实现“考虑平等”)

This way allows associative arrays whose members are ordered differently - e.g. they'd be considered equal in every language but php :)

这种方法允许关联数组的成员顺序不同——例如,除了php以外,其他语言都认为它们是相等的。

// recursive ksort
function rksort($a) {
  if (!is_array($a)) {
    return $a;
  }
  foreach (array_keys($a) as $key) {
    $a[$key] = ksort($a[$key]);
  }
  // SORT_STRING seems required, as otherwise
  // numeric indices (e.g. "0") aren't sorted.
  ksort($a, SORT_STRING);
  return $a;
}


// Per http://tools.ietf.org/html/rfc6902#section-4.6
function considered_equal($a1, $a2) {
  return json_encode(rksort($a1)) === json_encode(rksort($a2));
}

#10


1  

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 array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.

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

#11


0  

Use php function array_diff(array1, array2);

使用php函数array_diff(array1, array2);

It will return a the difference between arrays. If its empty then they're equal.

它将返回数组之间的差值。如果它是空的,那么它们是相等的。

example:

例子:

$array1 = array(
    'a' => 'value1',

    'b' => 'value2',

    'c' => 'value3'
 );

$array2 = array(
    'a' => 'value1',

    'b' => 'value2',

    'c' => 'value4'
 );

$diff = array_diff(array1, array2);

var_dump($diff); 

//it will print array = (0 => ['c'] => 'value4' ) 

Example 2:

示例2:

$array1 = array(
    'a' => 'value1',

    'b' => 'value2',

    'c' => 'value3',
 );

$array2 = array(
    'a' => 'value1',

    'b' => 'value2',

    'c' => 'value3',
 );

$diff = array_diff(array1, array2);

var_dump($diff); 

//it will print empty; 

#12


0  

Syntax problem on your arrays

数组的语法问题

$array1 = array(
    'a' => 'value1',
    'b' => 'value2',
    'c' => 'value3',
 );

$array2 = array(
    'a' => 'value1',
    'b' => 'value2',
    'c' => 'value3',
 );

$diff = array_diff($array1, $array2);

var_dump($diff); 

#1


362  

$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 ===.

不等式操作符为!=,而非标识操作符为!=,以匹配等式操作符==,标识操作符=== =。

#2


51  

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 == $b or $a === $b fails, 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").

数组(0 = > " x ",1 = > " y ")和数组(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建议),因为它可以提高速度。

#3


29  

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";

#4


9  

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

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

#5


9  

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.php Note 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注意,例如==也检查数组中元素的类型和顺序是否相同。

#6


7  

You can use array_diff_assoc to check for the differences between the two.

您可以使用array_diff_assoc检查两者之间的差异。

#7


2  

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;
}

#8


2  

Working 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) );
}

#9


1  

One way: (implementing 'considered equal' for http://tools.ietf.org/html/rfc6902#section-4.6)

一种方法:(为http://tools.ietf.org/html/rfc6902# sec-4.6实现“考虑平等”)

This way allows associative arrays whose members are ordered differently - e.g. they'd be considered equal in every language but php :)

这种方法允许关联数组的成员顺序不同——例如,除了php以外,其他语言都认为它们是相等的。

// recursive ksort
function rksort($a) {
  if (!is_array($a)) {
    return $a;
  }
  foreach (array_keys($a) as $key) {
    $a[$key] = ksort($a[$key]);
  }
  // SORT_STRING seems required, as otherwise
  // numeric indices (e.g. "0") aren't sorted.
  ksort($a, SORT_STRING);
  return $a;
}


// Per http://tools.ietf.org/html/rfc6902#section-4.6
function considered_equal($a1, $a2) {
  return json_encode(rksort($a1)) === json_encode(rksort($a2));
}

#10


1  

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 array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.

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

#11


0  

Use php function array_diff(array1, array2);

使用php函数array_diff(array1, array2);

It will return a the difference between arrays. If its empty then they're equal.

它将返回数组之间的差值。如果它是空的,那么它们是相等的。

example:

例子:

$array1 = array(
    'a' => 'value1',

    'b' => 'value2',

    'c' => 'value3'
 );

$array2 = array(
    'a' => 'value1',

    'b' => 'value2',

    'c' => 'value4'
 );

$diff = array_diff(array1, array2);

var_dump($diff); 

//it will print array = (0 => ['c'] => 'value4' ) 

Example 2:

示例2:

$array1 = array(
    'a' => 'value1',

    'b' => 'value2',

    'c' => 'value3',
 );

$array2 = array(
    'a' => 'value1',

    'b' => 'value2',

    'c' => 'value3',
 );

$diff = array_diff(array1, array2);

var_dump($diff); 

//it will print empty; 

#12


0  

Syntax problem on your arrays

数组的语法问题

$array1 = array(
    'a' => 'value1',
    'b' => 'value2',
    'c' => 'value3',
 );

$array2 = array(
    'a' => 'value1',
    'b' => 'value2',
    'c' => 'value3',
 );

$diff = array_diff($array1, $array2);

var_dump($diff);