比较php中的数组,而不关心顺序

comparing arrays in php, without caring for the order(比较php中的数组,而不关心顺序)
本文介绍了比较php中的数组,而不关心顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有两个数组,$a 和 $b,需要检查它们是否包含完全相同的元素(与顺序无关).我正在考虑使用

if (sizeof($a)==sizeof($b) AND array_diff($a,$b)==array()){}

但是我是 PHP 新手,所以我想知道:有没有更好的方法?

由于我需要将它们用作集合,也许我根本不应该使用数组而是其他东西.

解决方案

接受的答案 is 是错误的.它会失败:https://3v4l.org/U8U5pp>

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

这是一个正确的解决方案:

函数包含OfTheSameValues(数组$a,数组$b){//检查两个数组的大小如果(计数($a)!==计数($b)){返回假;}foreach ($b as $key => $bValue) {//检查期望值是否存在于数组中if (!in_array($bValue, $a, true)) {返回假;}//检查期望值在两个数组中出现的次数相同if (count(array_keys($a, $bValue, true)) !== count(array_keys($b, $bValue, true))) {返回假;}}返回真;}

加上相当广泛的单元测试:https://3v4l.org/m6lHv

 $bValue) {//检查期望值是否存在于数组中if (!in_array($bValue, $a, true)) {返回假;}//检查期望值在两个数组中出现的次数相同if (count(array_keys($a, $bValue, true)) !== count(array_keys($b, $bValue, true))) {返回假;}}返回真;}it('由相同的值组成',包含OfTheSameValues([1],[1])===真);it('由相同的值组成',包含OfTheSameValues([1, 1], [1, 1]) === true);it('由相同的值组成',包含OfTheSameValues(['1', 1], ['1', 1]) === true);it('由相同的值组成',包含OfTheSameValues(['1', 1], [1, '1']) === true);it('由相同的值组成',包含OfTheSameValues([1, '1'], ['1', 1]) === true);it('由相同的值组成',包含OfTheSameValues([1, '1'], [1, '1']) === true);it('由相同的值组成',包含OfTheSameValues(['x' => 1],['x' => 1])===真);it('由相同的值组成',包含OfTheSameValues(['x' => 1],['y' => 1])===真);it('由相同的值组成',包含OfTheSameValues(['y' => 1],['x' => 1])===真);it('由相同的值组成',包含SameValues(['x' => 1, 'y' => 1], ['x' => 1, 'y' => 1]) === true);it('由相同的值组成',包括SameValues(['y' => 1, 'x' => 1], ['x' => 1, 'y' => 1]) === true);it('由相同的值组成',包含SameValues(['x' => 1, 'y' => 1], ['y' => 1, 'x' => 1]) === true);it('由相同的值组成',包含SameValues(['y' => 1, 'x' => 1], ['y' => 1, 'x' => 1]) === true);it('由相同的值组成',包括SameValues(['x' => 2, 'y' => 1], ['x' => 1, 'y' => 2]) === true);it('不包含相同的值',包含OfTheSameValues([1],[2])===假);it('不包含相同的值',包含OfTheSameValues(['1'], [1]) === false);it('不包含相同的值',包含OfTheSameValues([1],['1'])===假);it('不包含相同的值',包含OfTheSameValues([1], [1, 1]) === false);it('不包含相同的值',包含OfTheSameValues([1, 1], [1]) === false);it('不包含相同的值',包含OfTheSameValues(['1', 1], [1, 1]) === false);it('不包含相同的值',包含OfTheSameValues([1, '1'], [1, 1]) === false);it('不包含相同的值',包含OfTheSameValues([1, 1], ['1', 1]) === false);it('不包含相同的值',包含OfTheSameValues([1, 1], [1, '1']) === false);it('不包含相同的值',包含OfTheSameValues(['1', '1'], [1, 1]) === false);it('不包含相同的值',包含OfTheSameValues(['1', '1'], ['1', 1]) === false);it('不包含相同的值',包含OfTheSameValues(['1', '1'], [1, '1']) === false);it('不包含相同的值',包含OfTheSameValues([1, 1], ['1', '1']) === false);it('不包含相同的值',包含OfTheSameValues(['1', 1], ['1', '1']) === false);it('不包含相同的值',包含OfTheSameValues([1, '1'], ['1', '1']) === false);it('不包含相同的值',包含OfTheSameValues(['x' => 1], ['x' => 2]) === false);it('不包含相同的值',包含SameValues(['x' => 1, 'y' => 1], ['x' => 1, 'y' => 2]) === false);it('不包含相同的值',包括SameValues(['x' => 1, 'y' => 1], ['x' => 2, 'y' => 1]) === false);it('不包含相同的值',包含SameValues(['x' => 2, 'y' => 1], ['x' => 1, 'y' => 1]) === false);

@update:

@ircmaxell 答案的广泛单元测试:https://3v4l.org/5ivgm

@Jon anwser 的广泛单元测试:https://3v4l.org/CrTgQ

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 (sizeof($a)==sizeof($b) AND array_diff($a,$b)==array())
{

}

But I am new to PHP, so I wonder: Is there a better way?

Since I need to use them as sets, maybe I should not use arrays at all but something else.

解决方案

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

<?php

// A unit testing framework in a tweet. https://gist.github.com/mathiasverraes/9046427
function it($m,$p){echo ($p?'✔︎':'✘')." It $m
"; if(!$p){$GLOBALS['f']=1;}}function done(){if(@$GLOBALS['f'])die(1);}

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;
}

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

这篇关于比较php中的数组,而不关心顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)