php 检查两个数组是否具有相同的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21138505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if two arrays have the same values
提问by Anna K.
[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 循环吗?我不认为这会很有效率。
回答by user1844933
sort($a);
sort($b);
if ($a==$b) {//equal}
回答by TwiNight
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 的回答更笼统。
回答by Conor Mancone
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 指出这仅适用于具有唯一值的数组的事实。
回答by whitebrow
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.phplike 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';
}
回答by Tamim
The best way will be using array_diffhttp://php.net/manual/en/function.array-diff.php
最好的方法是使用array_diffhttp://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);
回答by seyfahni
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.
由于给定的答案中没有一个是完全独立于键的具有重复值的工作(例如[1,1,2]equals [1,2,2]),因此我自己编写了。
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.
尽管该问题询问了 foreach-free 变体,但我找不到任何满足我要求的解决方案,而无需循环。此外,大多数其他使用的函数也在内部使用循环。
回答by jishnu
Say, if you have two arrays defined like this:
说,如果你有两个这样定义的数组:
$array1 = array(2,5,3);
$array2 = array(5,2,3);
Then you can use this piece of code to judge whether they equal:
那么就可以用这段代码来判断它们是否相等:
if(array_diff($array1,$array2) === array_diff($array2,$array1) &&count($array1)==count($array2))
{
echo 'Equal';
}
else
{
echo 'Not equal';
}
回答by IdiakosE Sunday
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.
基本上,在这里遍历其中一个数组(我假设它们的大小相同)。然后检查它是否存在于另一个数组中。然后我减少了结果。
回答by Rubin Porwal
$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.";
}

