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

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

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

    echo "enter";
}

当前回答

一种方法:(为https://www.rfc-editor.org/rfc/rfc6902#section-4.6实现“考虑平等”)

这种方式允许关联数组的成员顺序不同——例如,除了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 https://www.rfc-editor.org/rfc/rfc6902#section-4.6
function considered_equal($a1, $a2) {
  return json_encode(rksort($a1)) === json_encode(rksort($a2));
}

其他回答

如果你想检查非关联数组,下面是解决方案:

$a = ['blog', 'company'];
$b = ['company', 'blog'];

(count(array_unique(array_merge($a, $b))) === count($a)) ? 'Equals' : 'Not Equals';
// Equals

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

$foo =serialize($array_foo);
$bar =serialize($array_bar);
if ($foo == $bar) echo "Foo and bar are equal";
$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.

参见数组操作符。

EDIT

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

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

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

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

数组的语法问题

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

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

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

var_dump($diff);