Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if two arrays have the same values

Tags:

arrays

php

[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.

like image 784
Anna K. Avatar asked Jan 15 '14 13:01

Anna K.


People also ask

How do you check if two arrays have the same value?

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.

How do I compare values in two arrays?

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.


1 Answers

sort($a); sort($b); if ($a===$b) {//equal} 
like image 159
user1844933 Avatar answered Oct 04 '22 23:10

user1844933