comparing arrays in php, without caring for the order

前端 未结 5 764
借酒劲吻你
借酒劲吻你 2020-12-10 03:42

I have two arrays, $a and $b here, and need to check if they contain exactly the same elements (independently of the order). I am thinking of using

if (sizeo         


        
5条回答
  •  清歌不尽
    2020-12-10 04:15

    The accepted answer is was wrong. It will would fail on: https://3v4l.org/U8U5p

    $a = ['x' => 1, 'y' => 2]; $b = ['x' => 1, 'y' => 1];

    Here is a correct solution:

    function consistsOfTheSameValues(array $a, array $b)
    {
        // check size of both arrays
        if (count($a) !== count($b)) {
            return false;
        }
    
        foreach ($b as $key => $bValue) {
    
            // check that expected value exists in the array
            if (!in_array($bValue, $a, true)) {
                return false;
            }
    
            // check that expected value occurs the same amount of times in both arrays
            if (count(array_keys($a, $bValue, true)) !== count(array_keys($b, $bValue, true))) {
                return false;
            }
    
        }
    
        return true;
    }
    

    Plus quite extensive unit tests: https://3v4l.org/m6lHv

     $bValue) {
    
            // check that expected value exists in the array
            if (!in_array($bValue, $a, true)) {
                return false;
            }
    
            // check that expected value occurs the same amount of times in both arrays
            if (count(array_keys($a, $bValue, true)) !== count(array_keys($b, $bValue, true))) {
                return false;
            }
    
        }
    
        return true;
    }
    
    it('consist of the same values',
        consistsOfTheSameValues([1], [1]) === true
    );
    
    it('consist of the same values',
        consistsOfTheSameValues([1, 1], [1, 1]) === true
    );
    
    it('consist of the same values',
        consistsOfTheSameValues(['1', 1], ['1', 1]) === true
    );
    
    it('consist of the same values',
        consistsOfTheSameValues(['1', 1], [1, '1']) === true
    );
    
    it('consist of the same values',
        consistsOfTheSameValues([1, '1'], ['1', 1]) === true
    );
    
    it('consist of the same values',
        consistsOfTheSameValues([1, '1'], [1, '1']) === true
    );
    
    it('consist of the same values',
        consistsOfTheSameValues(['x' => 1], ['x' => 1]) === true
    );
    
    it('consist of the same values',
        consistsOfTheSameValues(['x' => 1], ['y' => 1]) === true
    );
    
    it('consist of the same values',
        consistsOfTheSameValues(['y' => 1], ['x' => 1]) === true
    );
    
    it('consist of the same values',
        consistsOfTheSameValues(['x' => 1, 'y' => 1], ['x' => 1, 'y' => 1]) === true
    );
    
    it('consist of the same values',
        consistsOfTheSameValues(['y' => 1, 'x' => 1], ['x' => 1, 'y' => 1]) === true
    );
    
    it('consist of the same values',
        consistsOfTheSameValues(['x' => 1, 'y' => 1], ['y' => 1, 'x' => 1]) === true
    );
    
    it('consist of the same values',
        consistsOfTheSameValues(['y' => 1, 'x' => 1], ['y' => 1, 'x' => 1]) === true
    );
    
    it('consist of the same values',
        consistsOfTheSameValues(['x' => 2, 'y' => 1], ['x' => 1, 'y' => 2]) === true
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues([1], [2]) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues(['1'], [1]) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues([1], ['1']) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues([1], [1, 1]) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues([1, 1], [1]) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues(['1', 1], [1, 1]) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues([1, '1'], [1, 1]) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues([1, 1], ['1', 1]) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues([1, 1], [1, '1']) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues(['1', '1'], [1, 1]) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues(['1', '1'], ['1', 1]) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues(['1', '1'], [1, '1']) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues([1, 1], ['1', '1']) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues(['1', 1], ['1', '1']) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues([1, '1'], ['1', '1']) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues(['x' => 1], ['x' => 2]) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues(['x' => 1, 'y' => 1], ['x' => 1, 'y' => 2]) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues(['x' => 1, 'y' => 1], ['x' => 2, 'y' => 1]) === false
    );
    
    it('does not consist of the same values',
        consistsOfTheSameValues(['x' => 2, 'y' => 1], ['x' => 1, 'y' => 1]) === false
    );
    

    @update:

    Extensive unit test of @ircmaxell answer: https://3v4l.org/5ivgm

    Extensive unit test of @Jon anwser: https://3v4l.org/CrTgQ

提交回复
热议问题