How to add to an associated array

I have the following array

$data = array('key1'=>'val1',
              'key2'=>'val2',
              'key3'=>'val3'
             );

I would like to add to the array.

$moreval = array('key4'=>'val4');
+3
source share
2 answers

You can use array_merge for this.

$data=array_merge($data, array('key4'=>'val4'));

If this was not associative, you can use array_push .

+1
source

you can just use:

$ data ['key4'] = 'val4';

0
source

Source: https://habr.com/ru/post/1785107/


All Articles