Lets say you are having a user provide information.
Array 1
But not all is required. So you have defaults.
Array 2
If you just want to keep the options that you expect and discard the rest you may use a combination of array_merge and array_intersect_key.
1,
'b' => null,
];
$mergedParams = array_merge(
$defaults,
array_intersect_key($options, $defaults)
);
return $mergedParams;
}
var_dump(foo([
'a' => 'keep me',
'c' => 'discard me'
]));
// => output
//
// array(2) {
// ["a"]=>
// string(7) "keep me"
// ["b"]=>
// NULL
// }
If you instead want to keep any extra key then array_merge($defaults, $options)
will do just fine.