Merge “Defaults” array with “Input” array? PHP Which Function?

后端 未结 5 1893
野的像风
野的像风 2021-02-14 15:43

Lets say you are having a user provide information.

Array 1

But not all is required. So you have defaults.

Array 2
         


        
5条回答
  •  再見小時候
    2021-02-14 16:22

    $defaults = array(
        'some_key_1'=>'default_value_1',
        'some_key_2'=>'default_value_2',
    );
    
    $inputs = array_merge($defaults, $inputs)
    

    Note that if the $inputs array contains keys not in the $defaults array they will be added in the result.

提交回复
热议问题