PHP:内置函数,用于检查两个Array值是否相等(忽略顺序)

时间:2022-12-02 14:04:19

Is there a built-in function for PHP for me to check whether two arrays contain the same values ( order not important?).

是否有PHP的内置函数来检查两个数组是否包含相同的值(顺序不重要?)。

For example, I want a function that returns me true for the following two inputs:

例如,我想要一个函数,对于以下两个输入返回true:

array('4','5','2') 
array('2','4','5')

Edit: I could have sorted the two arrays and compare them, but as I am such a lazy guy, I would still prefer a one-liner that I can pull out and use.

编辑:我可以对两个阵列进行排序并对它们进行比较,但由于我是一个懒惰的家伙,我仍然更喜欢我可以拔出并使用的单线程。

7 个解决方案

#1


29  

array_diff looks like an option:

array_diff看起来像一个选项:

function array_equal($a1, $a2) {
  return !array_diff($a1, $a2) && !array_diff($a2, $a1);
}

or as an oneliner in your code:

或者作为代码中的oneliner:

if(!array_diff($a1, $a2) && !array_diff($a2, $a1)) doSomething();

#2


7  

The best solution is to sort both array and then compare them:

最好的解决方案是对两个数组进行排序,然后比较它们:

$a = array('4','5','2');
$b = array('2','4','5');
sort($a);
sort($b);
var_dump($a === $b);

As a function:

作为一个功能:

function array_equal($a, $b, $strict=false) {
    if (count($a) !== count($b)) {
        return false;
    }
    sort($a);
    sort($b);
    return ($strict && $a === $b) || $a == $b;
}

Here’s another algorithm looking for each element of A if it’s in B:

这是另一个算法,如果它在B中,则查找A的每个元素:

function array_equal($a, $b, $strict=false) {
    if (count($a) !== count($b)) {
        return false;
    }
    foreach ($a as $val) {
        $key = array_search($val, $b, $strict);
        if ($key === false) {
            return false;
        }
        unset($b[$key]);
    }
    return true;
}

But that has a complexity of O(n^2). So you better use the sorting method.

但是它具有O(n ^ 2)的复杂性。所以你最好使用排序方法。

#3


3  

The array_diff() method above won't work.

上面的array_diff()方法不起作用。

php.net's manual says that array_diff() does this:

php.net的手册说array_diff()执行此操作:

"Returns an array containing all the entries from array1 that are not present in any of the other arrays."

“返回一个数组,其中包含array1中任何其他数组中不存在的所有条目。”

So the actual array_diff() method would be:

所以实际的array_diff()方法是:

function array_equal($array1, $array2)
{
   $diff1 = array_diff($array1, $array2);
   $diff2 = array_diff($array2, $array1);

   return
   (
      (count($diff1) === 0) &&
      (count($diff2) === 0)
   );
}

However I go with the sort method :D

但是我选择排序方法:D

#4


2  

You can use array_diff.

你可以使用array_diff。

$a = array('4','5','2');
$b = array('2','4','5');

if(count(array_diff($a, $b)) == 0) {
  // arrays contain the same elements
} else {
  // arrays contain different elements
}

However, a problem with this approach is that arrays can contain duplicate elements, and still match.

但是,这种方法的一个问题是数组可以包含重复的元素,并且仍然匹配。

#5


1  

You only need to compare one-way using array_diff() and use count() for the inverted relationship.

您只需要使用array_diff()进行单向比较,并使用count()进行反转关系。

if (count($a1) == count($a2) && !array_diff($a1, $a2)) {
    // equal arrays
}

#6


0  

You can use array_intersect() instead of array_diff():

您可以使用array_intersect()而不是array_diff():

$a = array('4','5','2');
$b = array('2','4','5');
$ca = count($a);
$cb = count($b);
$array_equal = ( $ca == $cb && $ca == count(array_intersect($a, $b)) );

Performance wise. solution, where two factors are important:

表现明智。解决方案,其中两个因素很重要:

  • the more often arrays are matching, the more array_intersect() is fast.
  • 数组匹配的次数越多,array_intersect()就越快。
  • the more arrays are big (more than 10 values), the more array_intersect() is fast.
  • 数组越大(超过10个值),array_intersect()就越快。

Depending on these factors, one method can be two or three time faster than the other. For big arrays with few (or no) matching combinations, or for little arrays with lots of matching, both methods are equivalent.

根据这些因素,一种方法可以比另一种方法快两到三倍。对于具有很少(或没有)匹配组合的大型数组,或者对于具有大量匹配的小数组,两种方法都是等效的。

However, the sort method is always faster, except in the case with little arrays with few or no matching combinations. In this case the array_diff() method is 30% faster.

但是,sort方法总是更快,除非少数阵列具有很少或没有匹配组合。在这种情况下,array_diff()方法的速度提高了30%。

#7


0  

If the arrays being compared consist of only strings and/or integers, array_count_values allows you to compare the arrays quickly (in O(n) time vs O(n log n) for sorting) by verifying that both arrays contain the same values and that each value occurs the same # of times in both arrays.

如果要比较的数组只包含字符串和/或整数,则array_count_values允许您通过验证两个数组包含相同的值来快速比较数组(在O(n)时间与O(n log n)中进行排序)每个值在两个数组中出现的次数相同。

if(array_count_values($a1) == array_count_values($a2)) {
    //arrays are equal
}

#1


29  

array_diff looks like an option:

array_diff看起来像一个选项:

function array_equal($a1, $a2) {
  return !array_diff($a1, $a2) && !array_diff($a2, $a1);
}

or as an oneliner in your code:

或者作为代码中的oneliner:

if(!array_diff($a1, $a2) && !array_diff($a2, $a1)) doSomething();

#2


7  

The best solution is to sort both array and then compare them:

最好的解决方案是对两个数组进行排序,然后比较它们:

$a = array('4','5','2');
$b = array('2','4','5');
sort($a);
sort($b);
var_dump($a === $b);

As a function:

作为一个功能:

function array_equal($a, $b, $strict=false) {
    if (count($a) !== count($b)) {
        return false;
    }
    sort($a);
    sort($b);
    return ($strict && $a === $b) || $a == $b;
}

Here’s another algorithm looking for each element of A if it’s in B:

这是另一个算法,如果它在B中,则查找A的每个元素:

function array_equal($a, $b, $strict=false) {
    if (count($a) !== count($b)) {
        return false;
    }
    foreach ($a as $val) {
        $key = array_search($val, $b, $strict);
        if ($key === false) {
            return false;
        }
        unset($b[$key]);
    }
    return true;
}

But that has a complexity of O(n^2). So you better use the sorting method.

但是它具有O(n ^ 2)的复杂性。所以你最好使用排序方法。

#3


3  

The array_diff() method above won't work.

上面的array_diff()方法不起作用。

php.net's manual says that array_diff() does this:

php.net的手册说array_diff()执行此操作:

"Returns an array containing all the entries from array1 that are not present in any of the other arrays."

“返回一个数组,其中包含array1中任何其他数组中不存在的所有条目。”

So the actual array_diff() method would be:

所以实际的array_diff()方法是:

function array_equal($array1, $array2)
{
   $diff1 = array_diff($array1, $array2);
   $diff2 = array_diff($array2, $array1);

   return
   (
      (count($diff1) === 0) &&
      (count($diff2) === 0)
   );
}

However I go with the sort method :D

但是我选择排序方法:D

#4


2  

You can use array_diff.

你可以使用array_diff。

$a = array('4','5','2');
$b = array('2','4','5');

if(count(array_diff($a, $b)) == 0) {
  // arrays contain the same elements
} else {
  // arrays contain different elements
}

However, a problem with this approach is that arrays can contain duplicate elements, and still match.

但是,这种方法的一个问题是数组可以包含重复的元素,并且仍然匹配。

#5


1  

You only need to compare one-way using array_diff() and use count() for the inverted relationship.

您只需要使用array_diff()进行单向比较,并使用count()进行反转关系。

if (count($a1) == count($a2) && !array_diff($a1, $a2)) {
    // equal arrays
}

#6


0  

You can use array_intersect() instead of array_diff():

您可以使用array_intersect()而不是array_diff():

$a = array('4','5','2');
$b = array('2','4','5');
$ca = count($a);
$cb = count($b);
$array_equal = ( $ca == $cb && $ca == count(array_intersect($a, $b)) );

Performance wise. solution, where two factors are important:

表现明智。解决方案,其中两个因素很重要:

  • the more often arrays are matching, the more array_intersect() is fast.
  • 数组匹配的次数越多,array_intersect()就越快。
  • the more arrays are big (more than 10 values), the more array_intersect() is fast.
  • 数组越大(超过10个值),array_intersect()就越快。

Depending on these factors, one method can be two or three time faster than the other. For big arrays with few (or no) matching combinations, or for little arrays with lots of matching, both methods are equivalent.

根据这些因素,一种方法可以比另一种方法快两到三倍。对于具有很少(或没有)匹配组合的大型数组,或者对于具有大量匹配的小数组,两种方法都是等效的。

However, the sort method is always faster, except in the case with little arrays with few or no matching combinations. In this case the array_diff() method is 30% faster.

但是,sort方法总是更快,除非少数阵列具有很少或没有匹配组合。在这种情况下,array_diff()方法的速度提高了30%。

#7


0  

If the arrays being compared consist of only strings and/or integers, array_count_values allows you to compare the arrays quickly (in O(n) time vs O(n log n) for sorting) by verifying that both arrays contain the same values and that each value occurs the same # of times in both arrays.

如果要比较的数组只包含字符串和/或整数,则array_count_values允许您通过验证两个数组包含相同的值来快速比较数组(在O(n)时间与O(n log n)中进行排序)每个值在两个数组中出现的次数相同。

if(array_count_values($a1) == array_count_values($a2)) {
    //arrays are equal
}