Skip to content

Instantly share code, notes, and snippets.

@ofca
Created June 14, 2013 13:26
Show Gist options
  • Save ofca/5781784 to your computer and use it in GitHub Desktop.
Save ofca/5781784 to your computer and use it in GitHub Desktop.
/**
* $defaults = array(
* 'a' => 2
* 'b' => 2
* );
* $untrusted = array(
* 'a' => 4,
* 'c' => 4
* );
* print_r(arrayMergeDefault($default, $unstrusted));
*
* // Result
* array('a' => 4, 'b' => 2);
*/
function arrayMergeDefault($default, $data) {
//Get data for which a default exists
$intersect = array_intersect_key($data, $default);
//Get defaults which are not present in data
$diff = array_diff_key($default, $data);
//Arrays have different keys, return the union of the two
return $diff + $intersect;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment