Created
July 22, 2014 22:16
-
-
Save donatj/0e22416775243adc7995 to your computer and use it in GitHub Desktop.
Similar Array Checker - Regardless of Key Order
This file contains hidden or 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
<?php | |
function arrays_are_similar( $aSide, $bSide ) { | |
$keys = array_unique(array_merge( | |
array_keys($aSide), | |
array_keys($bSide) | |
)); | |
foreach( $keys as $key ) { | |
if( !array_key_exists($key, $aSide) || !array_key_exists($key, $bSide) ) { | |
return false; | |
} | |
$aSideValue = $aSide[$key]; | |
$bSideValue = $bSide[$key]; | |
if( is_array($aSideValue) && is_array($bSideValue) ) { | |
if( !$this->arrays_are_similar($aSideValue, $bSideValue) ) { | |
return false; | |
} | |
} elseif( !is_array($aSideValue) && !is_array($bSideValue) ) { | |
if( $aSideValue !== $bSideValue ) { | |
return false; | |
} | |
} else { | |
return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment