Skip to content

Instantly share code, notes, and snippets.

@itsKnight847
Created February 1, 2018 10:20
Show Gist options
  • Save itsKnight847/af7d502d7f264f725aa168cc5cb29cb1 to your computer and use it in GitHub Desktop.
Save itsKnight847/af7d502d7f264f725aa168cc5cb29cb1 to your computer and use it in GitHub Desktop.
recursive array check PHP
/**
* check recursive array and spot the diffrence
* @param $_first_array
* @param $_second_array
* @return array
*/
private function recursive_array_check($_first_array, $_second_array) {
$diff = [];
foreach($_first_array as $k => $v) {
if(is_array($v) && isset($_second_array[$k])){
$_diff = self::recursive_array_check($v, $_second_array[$k]);
if( !empty($_diff) ) {
$diff[$k] = $_diff;
}
} else if(is_string($v) && !in_array($v, $_second_array)) {
$diff[$k] = $v . " missing from second array";
} else if(!is_numeric($k) && !array_key_exists($k, $_second_array)) {
$diff[$k] = "Missing from second array";
}
}
return $diff;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment