Lets say you are having a user provide information.
Array 1
But not all is required. So you have defaults.
Array 2
I think what you are looking for is array_replace_recursive
, especially for the case when your "defualts" may be an associative array more than one level deep.
$finalArray = array_replace_recursive(array $defaults, array $inputOptions)
heres an example that takes an optional array of options to a function and does some processing based on the result of those options "opts
" and the defaults
which you specify:
function do_something() {
$args = func_get_args();
$opts = $args[0] ? $args[0] : array();
$defaults = array(
"second_level" => array(
"key1" => "val1",
"key2" => "val2"
),
"key1" => "val1",
"key2" => "val2",
"key3" => "val3"
);
$params = array_replace_recursive($defaults, $opts);
// do something with these merged parameters
}
The php.net reference document is here