tags:

views:

45

answers:

4

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

        $new_input['name']=array('type' => 'text', 'label' => 'First name', 'show' => true, 'required' => true);
       array_push($options['inputs'], $new_input);

However, instead of 'name' as the key in adds a number. Is there another way to do it?

+4  A: 
$options['inputs']['name'] = $new_input['name'];
webbiedave
A: 
$new_input = array('type' => 'text', 'label' => 'First name', 'show' => true, 'required' => true);
$options['inputs']['name'] = $new_input;
Ryan Kinal
A: 

WebbieDave's solution will work. If you don't want to overwrite anything that might already be at 'name', you can also do something like this:

$options['inputs']['name'][] = $new_input['name'];

Curtis
A: 

If $new_input may contain more than just a 'name' element you may want to use array_merge.

$new_input = array('name'=>array(), 'details'=>array());
$new_input['name'] = array('type'=>'text', 'label'=>'First name'...);
$options['inputs'] = array_merge($options['inputs'], $new_input);
thetaiko