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

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

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

    echo "enter";
}

当前回答

如果你想生成一份详细的报告,你可以使用这样的东西:

function deepCompare(Array $a, Array $b, string $parentAKey, string $parentBKey, bool $compareInverted = true, bool $compareValues = true, string $log = '')
{
    foreach ($a as $aKey => $aValue) {
        $fullAKey = implode('.', [$parentAKey, $aKey]);
        $fullBKey = implode('.', [$parentBKey, $aKey]);
        if (! isset($b[$aKey])) {
            $log .= "⍰ {$fullAKey} has no equivalent {$fullBKey}\n";
        } else {
            $bValue = $b[$aKey];
            if (is_array($aValue)) {
                $log = deepCompare($aValue, $bValue, $fullAKey, $fullBKey, false, $compareValues, $log);
            } else {
              if ($compareValues) {
                  if ($aValue != $bValue) {
                      $log .= "≠ {$fullAKey} value differs from {$fullBKey}\n";
                  }
              }
            }
        }
    }
    if ($compareInverted) {
        $log = deepCompare($b, $a, $parentBKey, $parentAKey, false, false, $log);
    }
    return $log;
}

下面是一个例子:

$november = [
  'site1' => [
    'id' => 15,
    'name' => 'Brazil',
    'extendedHours' => 454,
  ],
  'site2' => [
    'id' => 43,
    'name' => 'Portugal',
    'extendedHours' => 448,
  ],
  'site3' => [
    'id' => 49,
    'name' => 'Spain',
    'extendedHours' => 0,
  ],  
  'totalExtendedHours' => 902,
];

$december = [
  'site1' => [
    'id' => 15,
    'name' => 'Brazil',
    'extendedHours' => 498,
  ],
  'site2' => [
    'id' => 43,
    'name' => 'Portugal',
    'extendedHours' => 409,
    'extraRequests' => 6,
  ],
  'totalExtendedHours' => 907,
  'totalExtraRequests' => 6,
];

echo deepCompare(
    $november,    -- origin array
    $december,    -- target array
    'Nov2022',    -- descriptive name of origin array
    'Dec2022',    -- descriptive name of target array
    true,         -- should also compare arrays in reverse order?
    true          -- should care about array values? (false = names only)
);

这个例子将输出:

≠ Nov2022.site1.extendedHours value differs from Dec2022.site1.extendedHours
≠ Nov2022.site2.extendedHours value differs from Dec2022.site2.extendedHours
⍰ Nov2022.site3 has no equivalent Dec2022.site3
≠ Nov2022.totalExtendedHours value differs from Dec2022.totalExtendedHours
⍰ Dec2022.site2.extraRequests has no equivalent Nov2022.site2.extraRequests
⍰ Dec2022.totalExtraRequests has no equivalent Nov2022.totalExtraRequests

我希望这能帮助到一些人。

其他回答

根据这一页。

注意:接受的答案适用于关联数组,但不适用于索引数组(下文解释)。如果你想比较它们中的任何一个,那么使用这个解决方案。此外,此函数可能不适用于多维数组(由于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建议),因为它可以提高速度。

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

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

}

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

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

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

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

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

例子:

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

示例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; 

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

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