Created
June 14, 2013 13:26
-
-
Save ofca/5781784 to your computer and use it in GitHub Desktop.
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
/** | |
* $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