109

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?

Crisoforo Gaspar
  • 3,740
  • 2
  • 21
  • 27
ryudice
  • 36,476
  • 32
  • 115
  • 163
  • 1
    It is not possible push an array into another array. I've tried all this options and the merge just added the array. I solved my problem with a Class. – Alex Benincasa Santos Jun 05 '17 at 23:29

12 Answers12

155
$options['inputs']['name'] = $new_input['name'];
webbiedave
  • 48,414
  • 8
  • 88
  • 101
75

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.

Heemanshu Bhalla
  • 3,603
  • 1
  • 27
  • 53
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
24

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

Ajmal Salim
  • 4,142
  • 2
  • 33
  • 41
8

i use php5.6

code:

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

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

print_r($person) 

output

Array( ["name"=>"mohammed", "age"=>30, "addr"=>"Sudan"] )
Feras Al Sous
  • 1,073
  • 1
  • 12
  • 23
ebnibrahem
  • 99
  • 1
  • 4
7

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
  • 3,931
  • 1
  • 19
  • 26
  • This doesn't work if you don't want to keep everything associative, e.g. without pushing other numbered arrays in between. Have a look at @Steven H below – brianlmerritt Aug 31 '17 at 09:25
5

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
  • 7,816
  • 2
  • 33
  • 49
3

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!

bool.dev
  • 17,508
  • 5
  • 69
  • 93
Steven H
  • 357
  • 2
  • 9
2

Just change few snippet(use array_merge function):-

  $options['inputs']=array_merge($options['inputs'], $new_input);
vineet
  • 13,832
  • 10
  • 56
  • 76
1
$new_input = array('type' => 'text', 'label' => 'First name', 'show' => true, 'required' => true);
$options['inputs']['name'] = $new_input;
Ryan Kinal
  • 17,414
  • 6
  • 46
  • 63
1

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.

Henry
  • 1,242
  • 1
  • 12
  • 10
0

You can try.

$options['inputs'] = $options['inputs'] + $new_input;
Adnan Ahmad
  • 848
  • 1
  • 13
  • 12
-1

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 )
Mamun Sabuj
  • 189
  • 1
  • 2
  • 6