I have two arrays and I have to compare them against each other:
$a = array('BRANCH','ADDRESS','MOBILE','NAME');
$b = array('BRANCH','ADDRESS','MOBILE','NAME');
$a == $b → true
But when the values are in a different order, this comparison doesn't work:
$a = array('BRANCH','ADDRESS','MOBILE','NAME');
$b = array('NAME','BRANCH','MOBILE','ADDRESS');
$a == $b → false
How do i compare these two arrays to see if both contain the same values, regardless of their order?
I would do array_diff() which check difference between two arrays.
$areEqual = array_diff($a, $b) === array_diff($b, $a);
or
$areEqual = !(array_diff($a, $b) || array_diff($b, $a));
sort($a);
sort($b);
if ($a==$b) {
echo "arraysAreEqual";
}