[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.
To check if two arrays have the same elements:Use the every() to check if the arrays contain the same element at the same index. The every method only returns true if the condition is met for all array elements.
Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.
sort($a); sort($b); if ($a===$b) {//equal}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With