I have an array of values that are required for my app. If the values are not provided, i’d like to fill them with default values. For example, if my app expects three values
$default_vals = array('foo','bar','baz');
$given_vals = array('faz',NULL);
I need result:
$combined = array('faz','bar','baz')
Basically fill in array indexes that are missing with the default one.
It’s almost 5:00 and brain fried. I thought this could easily be done with an array_function() like array_intersect() or merge().
Thanks.
Edit: By the way..the $given_vals could contain the same value as the $default, so solution should work even if this is the case.
You need to filter the
NULLs out of the input first, then you can do a so called array union (the array union operator is+, see Array operatorsDocs):Take care that
array_filterDocs will remove as well values likeFALSEor an empty string. So I’m not 100% sure if that suits your needs. In case not, make it more specific: