PHP - Check if two arrays are equal

后端 未结 15 1925
说谎
说谎 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:54
    if (array_diff($a,$b) == array_diff($b,$a)) {
      // Equals
    }
    
    if (array_diff($a,$b) != array_diff($b,$a)) {
      // Not Equals
    }
    

    From my pov it's better to use array_diff than array_intersect because with checks of this nature the differences returned commonly are less than the similarities, this way the bool conversion is less memory hungry.

    Edit Note that this solution is for plain arrays and complements the == and === one posted above that is only valid for dictionaries.

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

    Short solution that works even with arrays which keys are given in different order:

    public static function arrays_are_equal($array1, $array2)
    {
        array_multisort($array1);
        array_multisort($array2);
        return ( serialize($array1) === serialize($array2) );
    }
    
    0 讨论(0)
  • 2020-11-22 11:57

    Another method for checking equality regardless of value order works by using http://php.net/manual/en/function.array-intersect.php, like so:

    $array1 = array(2,5,3);
    $array2 = array(5,2,3);
    if($array1 === array_intersect($array1, $array2) && $array2 === array_intersect($array2, $array1)) {
        echo 'Equal';
    } else {
        echo 'Not equal';
    }
    

    Here's a version that works also with multidimensional arrays using http://php.net/manual/en/function.array-uintersect.php:

    $array1 = array(
        array(5, 2),
        array(3, 6),
        array(2, 9, 4)
    );
    $array2 = array(
        array(3, 6),
        array(2, 9, 4),
        array(5, 2)
    );
    
    if($array1 === array_uintersect($array1, $array2, 'compare') && $array2 === array_uintersect($array2, $array1, 'compare')) {
        echo 'Equal';
    } else {
        echo 'Not equal';
    }
    
    function compare($v1, $v2) {
        if ($v1===$v2) {
            return 0;
        }
        if ($v1 > $v2) return 1;
        return -1;
    }
    
    0 讨论(0)
  • 2020-11-22 11:58

    Compare them as other values:

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

    You can read about all array operators here: http://php.net/manual/en/language.operators.array.php Note for example that === also checks that the types and order of the elements in the arrays are the same.

    0 讨论(0)
  • 2020-11-22 12:00

    Try serialize. This will check nested subarrays as well.

    $foo =serialize($array_foo);
    $bar =serialize($array_bar);
    if ($foo == $bar) echo "Foo and bar are equal";
    
    0 讨论(0)
  • 2020-11-22 12:00

    If you want to check non associative arrays, here is the solution:

    $a = ['blog', 'company'];
    $b = ['company', 'blog'];
    
    (count(array_unique(array_merge($a, $b))) === count($a)) ? 'Equals' : 'Not Equals';
    // Equals
    
    0 讨论(0)
提交回复
热议问题