我想检查两个数组是否相等。我的意思是:相同的大小,相同的索引,相同的值。我该怎么做呢?

根据用户的建议使用!==,如果数组中至少有一个元素是不同的,我希望下面的代码会打印enter,但实际上并不是这样。

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

    echo "enter";
}

当前回答

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

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

你可以在这里阅读所有数组操作符: http://php.net/manual/en/language.operators.array.php 例如,请注意===还检查数组中元素的类型和顺序是否相同。

其他回答

比较两个数组是否相等的正确方法是使用严格相等(===),这是递归比较。现有的答案无法递归地对任意数组(具有任意深度和顺序的数组,包含顺序数组和关联数组的混合)进行排序,因此无法处理任意数组的比较。顺序数组是具有顺序键(0,1,2,3…)的关联数组,而关联数组没有顺序键。

要对这些任意数组排序,我们必须:

向下遍历没有更多子数组的叶节点 通过序列化顺序数组,然后排序它们(以消除必须使用自定义比较器的需要) 按键对关联数组排序

下面的代码实现了上述解决方案。欢迎对代码进行改进。

function recur_sort( &$array ) {
    foreach ( $array as &$value ) {
       if ( is_array( $value ) ) recur_sort( $value );
    }

    if ( is_sequential_array( $array ) ) {
        $array = array_map( function( $el ) { return json_encode( $el ); }, $array  );
        sort( $array, SORT_STRING );
        $array = array_map( function( $el ) { return json_decode( $el, true ); }, $array  );
        return;
    } else {
        return ksort( $array );
    }
}

function is_sequential_array(Array &$a) {
    $n = count($a);
    for($i=0; $i<$n; $i++) {
        if(!array_key_exists($i, $a)) {
            return false;
        }
    }
    return true;
}

示例(在PHPUnit中):

//A stricter and recursive assertEqualsCanonicalizing
public function assertSameCanonicalizing( $expected, $actual ) {
    recur_sort( $expected );
    recur_sort( $actual );
    $this->assertSame( $expected, $actual );
}

数组的语法问题

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

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

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

var_dump($diff); 

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

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

下面的解决方案使用可作为回调传递的自定义相等函数。注意,它不检查数组的顺序。

trait AssertTrait
{
    /**
     * Determine if two arrays have the same elements, possibly in different orders. Elements comparison function must be passed as argument.
     *
     * @param array<mixed> $expected
     * @param array<mixed> $actual
     *
     * @throws InvalidArgumentException
     */
    public static function assertArraysContainSameElements(array $expected, array $actual, callable $comparisonFunction): void
    {
        Assert::assertEquals(\count($expected), \count($actual));

        self::assertEveryElementOfArrayIsInAnotherArrayTheSameAmountOfTimes($expected, $actual, $comparisonFunction);
        self::assertEveryElementOfArrayIsInAnotherArrayTheSameAmountOfTimes($actual, $expected, $comparisonFunction);
    }

    /**
     * @param array<mixed> $needles
     * @param array<mixed> $haystack
     *
     * @throws InvalidArgumentException
     */
    private static function assertEveryElementOfArrayIsInAnotherArrayTheSameAmountOfTimes(
        array $needles,
        array $haystack,
        callable $comparisonFunction
    ): void {
        Assert::assertLessThanOrEqual(\count($needles), \count($haystack));

        foreach ($needles as $expectedElement) {
            $matchesOfExpectedElementInExpected = \array_filter(
                $needles,
                static fn($element): bool => $comparisonFunction($expectedElement, $element),
            );

            $matchesOfExpectedElementInActual = \array_filter(
                $haystack,
                static fn($element): bool => $comparisonFunction($expectedElement, $element),
            );

            Assert::assertEquals(\count($matchesOfExpectedElementInExpected), \count($matchesOfExpectedElementInActual));
        }
    }
}

我通常在数据库集成测试中使用它,当我想确保返回预期的元素,但我不关心排序。

根据这一页。

注意:接受的答案适用于关联数组,但不适用于索引数组(下文解释)。如果你想比较它们中的任何一个,那么使用这个解决方案。此外,此函数可能不适用于多维数组(由于array_diff函数的性质)。

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

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

这是因为上面的意思是:

阵列(0 = > " x ", 1 = > " y ")和数组(0 = > " y ", 1 = > " x ")。

要解决这个问题,请使用:

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

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