Push item to associative array in PHP

后端 未结 12 1443
野趣味
野趣味 2020-12-04 22:47

I\'ve been trying to push an item to an associative array like this:

$new_input[\'name\'] = array(
    \'type\' => \'text\', 
    \'label\' => \'First          


        
12条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 23:49

    There is a better way to do this:

    If the array $arr_options contains the existing array.

    $arr_new_input['name'] = [
        'type' => 'text', 
        'label' => 'First name', 
        'show' => true, 
        'required' => true
    ];
    
    $arr_options += $arr_new_input;
    

    Warning: $arr_options must exist. if $arr_options already has a ['name'] it wil be overwritten.

    Hope this helps.

提交回复
热议问题