phparrays

PHP - Check if two arrays are equal


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.

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

    echo "enter";
}

Solution

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

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