Created
December 24, 2012 11:17
-
-
Save musaid/4368875 to your computer and use it in GitHub Desktop.
A simple function to check if arrays are similar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Determine if two associative arrays are similar | |
* | |
* Both arrays must have the same indexes with identical values | |
* without respect to key ordering | |
* | |
* @param array $a | |
* @param array $b | |
* @return bool | |
*/ | |
function arrays_are_similar($a, $b) { | |
// if the indexes don't match, return immediately | |
if (count(array_diff_assoc($a, $b))) { | |
return false; | |
} | |
// we know that the indexes, but maybe not values, match. | |
// compare the values between the two arrays | |
foreach($a as $k => $v) { | |
if ($v !== $b[$k]) { | |
return false; | |
} | |
} | |
// we have identical indexes, and no unequal values | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment