PHP - Check if two arrays are equal

后端 未结 15 1923
说谎
说谎 2020-11-22 11:26

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 th

相关标签:
15条回答
  • 2020-11-22 11:43

    array_diff — Computes the difference of arrays

    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.

    0 讨论(0)
  • 2020-11-22 11:44

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

    0 讨论(0)
  • 2020-11-22 11:45
    function compareIsEqualArray(array $array1,array $array):bool
    {
    
       return (array_diff($array1,$array2)==[] && array_diff($array2,$array1)==[]);
    
    }
    
    0 讨论(0)
  • 2020-11-22 11:48

    Here is the example how to compare to arrays and get what is different between them.

    $array1 = ['1' => 'XXX', 'second' => [
                'a' => ['test' => '2'],
                'b' => 'test'
            ], 'b' => ['no test']];
    
            $array2 = [
                '1' => 'XX',
                'second' => [
                    'a' => ['test' => '5', 'z' => 5],
                    'b' => 'test'
                ],
                'test'
            ];
    
    
            function compareArrayValues($arrayOne, $arrayTwo, &$diff = [], $reversed = false)
            {
                foreach ($arrayOne as $key => $val) {
                    if (!isset($arrayTwo[$key])) {
                        $diff[$key] = 'MISSING IN ' . ($reversed ? 'FIRST' : 'SECOND');
                    } else if (is_array($val) && (json_encode($arrayOne[$key]) !== json_encode($arrayTwo[$key]))) {
                        compareArrayValues($arrayOne[$key], $arrayTwo[$key], $diff[$key], $reversed);
                    } else if ($arrayOne[$key] !== $arrayTwo[$key]) {
                        $diff[$key] = 'DIFFERENT';
                    }
                }
            }
    
            $diff = [];
            $diffSecond = [];
    
            compareArrayValues($array1, $array2, $diff);
            compareArrayValues($array2, $array1, $diffSecond, true);
    
            print_r($diff);
            print_r($diffSecond);
    
            print_r(array_merge($diff, $diffSecond));
    

    Result:

    Array
    (
        [0] => DIFFERENT
        [second] => Array
            (
                [a] => Array
                    (
                        [test] => DIFFERENT
                        [z] => MISSING IN FIRST
                    )
    
            )
    
        [b] => MISSING IN SECOND
        [1] => DIFFERENT
        [2] => MISSING IN FIRST
    )
    
    0 讨论(0)
  • 2020-11-22 11:49
    $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 ===.

    0 讨论(0)
  • 2020-11-22 11:49

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

    This way allows associative arrays whose members are ordered differently - e.g. they'd be considered equal in every language but 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));
    }
    
    0 讨论(0)
提交回复
热议问题