Check if two arrays have the same values

后端 未结 9 1319
既然无缘
既然无缘 2020-12-15 02:45
[2,5,3]    

[5,2,3]

They are equal because they have the same values, but not in the same order. Can I find out that without using a foreach loop

相关标签:
9条回答
  • 2020-12-15 03:21

    This is a bit late to the party but in hopes that it will be useful:

    If you are sure the arrays both only contain strings or both only contain integers, then array_count_values($a) == array_count_values($b) has better time complexity. However, user1844933's answer is more general.

    0 讨论(0)
  • 2020-12-15 03:21

    The best way will be using array_diff http://php.net/manual/en/function.array-diff.php

    $arr1 = [2,5,3];
    $arr2 = [5,2,3];
    
    $isEqual = array_diff($arr1,$arr2) === array_diff($arr2,$arr1);
    
    0 讨论(0)
  • 2020-12-15 03:21

    I came across this problem and solve it thus: I needed to ensure that two objects had the same fields So

    const expectedFields = ['auth', 'message'];
    const receivedFields = Object.keys(data);
    const everyItemexists = expectedFields.map(i => receivedFields.indexOf(i) > -1);
    const condition = everyItemexists.reduce((accumulator, item) => item && accumulator, true);
    

    Basically, go through one of the arrays, here (I'm assuming there are of the same size though). Then check if its exists in the other array. Then i reduce the result of that.

    0 讨论(0)
  • 2020-12-15 03:24

    Coming to this party late. I had the same question but didn't want to sort, which was the immediate answer I knew would work. I came up with this simple one-liner which only works for arrays of unique values:

    $same = ( count( $a ) == count( $b ) && !array_diff( $a, $b ) )
    

    It's also about a factor of 5 faster than the sort option. Not that either is especially slow, so I would say it is more about your personal preferences and which one you think is more clear. Personally I would rather not sort.

    Edit: Thanks Ray for pointing out the fact that this only works with arrays with unique values.

    0 讨论(0)
  • 2020-12-15 03:29
    $array1 = array(2,5,3);
    $array2 = array(5,2,3);
    $result = array_diff($array1, $array2);
    if(empty($result))
    {
       echo "Both arrays are equal.";
    }
    else
    {
       echo "Both arrays are different.";
    }
    
    0 讨论(0)
  • 2020-12-15 03:31

    As none of the given answers that are completely key-independent work with duplicated values (like [1,1,2] equals [1,2,2]) I've written my own.

    This variant does not work with multi-dimensional arrays. It does check whether two arrays contain the exactly same values, regardless of their keys and order without modifying any of the arguments.

    function array_equals(array $either, array $other) : bool {
        $copy = $either;
        foreach ($other as $element) {
            $key = array_search($element, $copy, true);
            if ($key === false) {
                return false;
            }
            unset($copy[$key]);
        }
        return empty($copy);
    }
    

    Although the question asked about a foreach-free variant, I couldn't find any solution that satisfied my requirements without a loop. Additionally most of the otherwise used functions use a loop internally too.

    0 讨论(0)
提交回复
热议问题