PHP比较两个数组的简便方法:相同的值,无论顺序如何

Just a note of previous research on SO:

At least I believe that:

Given the example:

$arr1 = [21, 23, 25];
$arr2 = [25, 21, 23];

It the easiest, nondestructive manner to compare these for equal values, regardless of order

$arr1s = $arr1;
$arr2s = $arr2;

sort($arr1s, SORT_NUMERIC);
sort($arr2s, SORT_NUMERIC);

$isSameValues = ($arr1s === $arr2s);

Or is there an easier, cleaner way to do this?

Use array_count_values, which is O(n) vs O(n log n) for sorting

$a = [1, 1, 3, 2];
$b = [1, 2, 2, 3];
var_dump (array_count_values($b) == array_count_values($a)); //false

Note: this only works with arrays where all values are strings or ints.

You were very near to the right solution! Here is your code fixed:

$arr1 = [21, 23, 25];
$arr2 = [25, 21, 23];

$arr1s = $arr1;
$arr2s = $arr2;

sort($arr1s, SORT_NUMERIC);
sort($arr2s, SORT_NUMERIC);

$isSameValues = ($arr1 == $arr2);

See it working here: http://sandbox.onlinephpfunctions.com/code/b737d3a9cf45e077a0d1f2c0195f389b81ace4a3

If the two arrays have same keys and values, a == is enough, as shown here:

enter image description here