How to compare 2 arrays with each other?
For example i have array("a", "b", "c")
and array("a", "c", "b")
It would return true when they're compared. But if one of the letters if not found in one of them it would return false. Order is not important.
views:
51answers:
4Tried it out. Didn't work as expected in my code.
Semas
2010-08-22 10:26:16
@Semas, that is because array_diff only returns entities from array1 that are missing from array2. You would also need to check `array_diff($array2, $array1)` for completeness.
Brandon Horsley
2010-08-22 11:38:59
+2
A:
$diff = array_diff($array1, $array2);
if(!count($diff)){
# is the same
}
Haim Evgi
2010-08-22 10:25:26
array_diff only returns entities from array1 that are missing from array2. You would also need to check `array_diff($array2, $array1)` for completeness. The sort solution is probably faster.
Brandon Horsley
2010-08-22 11:40:20
A:
You can use:
if (empty(array_diff($array1, $array2)) {
// do something
}
Alec Smart
2010-08-22 10:27:58
This would not work anyway, array_diff only returns entities from array1 that are missing from array2. You would also need to check `array_diff($array2, $array1)` for completeness.
Brandon Horsley
2010-08-22 11:41:05