Push item to associative array in PHP

PhpArrays

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

Instead of array_push(), use array_merge()

It will merge two arrays and combine their items in a single array.

Example Code -

$existing_array = array('a'=>'b', 'b'=>'c');
$new_array = array('d'=>'e', 'f'=>'g');

$final_array=array_merge($existing_array, $new_array);

Its returns the resulting array in the final_array. And results of resulting array will be -

array('a'=>'b', 'b'=>'c','d'=>'e', 'f'=>'g')

Please review this link, to be aware of possible problems.

Solution 3 - Php

This is a cool function

function array_push_assoc($array, $key, $value){
   $array[$key] = $value;
   return $array;
}

Just use

$myarray = array_push_assoc($myarray, 'h', 'hello');

Credits & Explanation

Solution 4 - Php

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'];

Solution 5 - Php

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);

Solution 6 - Php

i use php5.6

code:

$person = ["name"=>"mohammed", "age"=>30];

$person['addr'] = "Sudan";

print_r($person) 

output

Array( ["name"=>"mohammed", "age"=>30, "addr"=>"Sudan"] )

Solution 7 - Php

Curtis's answer was very close to what I needed, but I changed it up a little.

Where he used:

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

I used:

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

Here's my actual code using a query from a DB:

while($row=mysql_fetch_array($result)){	
    $dtlg_array[]['dt'] = $row['dt'];
    $dtlg_array[]['lat'] = $row['lat'];
    $dtlg_array[]['lng'] = $row['lng'];
}

Thanks!

Solution 8 - Php

Just change few snippet(use array_merge function):-

  $options['inputs']=array_merge($options['inputs'], $new_input);

Solution 9 - Php

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

Solution 10 - Php

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.

Solution 11 - Php

You can try.

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

Solution 12 - Php

You can use array_merge($array1, $array2) to merge the associative array. Example:

$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));

Output:

Array ( [0] => red [1] => green [2] => blue [3] => yellow )

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionryudiceView Question on Stackoverflow
Solution 1 - PhpwebbiedaveView Answer on Stackoverflow
Solution 2 - PhpMurtaza Khursheed HussainView Answer on Stackoverflow
Solution 3 - PhpAjmal SalimView Answer on Stackoverflow
Solution 4 - PhpCurtisView Answer on Stackoverflow
Solution 5 - PhpthetaikoView Answer on Stackoverflow
Solution 6 - PhpebnibrahemView Answer on Stackoverflow
Solution 7 - PhpSteven HView Answer on Stackoverflow
Solution 8 - PhpvineetView Answer on Stackoverflow
Solution 9 - PhpRyan KinalView Answer on Stackoverflow
Solution 10 - PhpHenryView Answer on Stackoverflow
Solution 11 - PhpAdnan AhmadView Answer on Stackoverflow
Solution 12 - PhpMamun SabujView Answer on Stackoverflow