检查两个数组是否具有相同的值

时间:2021-01-21 12:19:06
[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 with in_array() ? I dont think it would be efficient.

它们是相等的,因为它们的值相同,但顺序不同。我可以在不使用in_array()的foreach循环的情况下找到它吗?我不认为这是有效的。

5 个解决方案

#1


58  

sort($a);
sort($b);
if ($a==$b) {//equal}

#2


16  

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.

如果您确定数组都只包含字符串,或者两个数组都只包含整数,那么array_count_values($a) == array_count_values($b)具有更好的时间复杂度。然而,user1844933的答案更一般。

#3


8  

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.

它也比排序选项快5倍。这两者都不是特别慢,所以我要说的是更多的是关于你的个人喜好和你认为哪个更清楚。我个人不喜欢排序。

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

编辑:感谢Ray指出这只适用于具有唯一值的数组。

#4


5  

If you don't want to sort arrays but just want to check equality regardless of value order use http://php.net/manual/en/function.array-intersect.php like so:

如果您不想对数组进行排序,而只想检查相等性,而不考虑值的顺序,请使用http://php.net/manual/en/function.array-intersect.php:

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

#5


-2  

$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.";
}

#1


58  

sort($a);
sort($b);
if ($a==$b) {//equal}

#2


16  

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.

如果您确定数组都只包含字符串,或者两个数组都只包含整数,那么array_count_values($a) == array_count_values($b)具有更好的时间复杂度。然而,user1844933的答案更一般。

#3


8  

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.

它也比排序选项快5倍。这两者都不是特别慢,所以我要说的是更多的是关于你的个人喜好和你认为哪个更清楚。我个人不喜欢排序。

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

编辑:感谢Ray指出这只适用于具有唯一值的数组。

#4


5  

If you don't want to sort arrays but just want to check equality regardless of value order use http://php.net/manual/en/function.array-intersect.php like so:

如果您不想对数组进行排序,而只想检查相等性,而不考虑值的顺序,请使用http://php.net/manual/en/function.array-intersect.php:

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

#5


-2  

$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.";
}