IMB IMB - 1 year ago 74
PHP Question

Other ways to add element in an array?

$a = ['foo' => 'bar'];

If I want to add new elements in this array I would usually write this

$a['foo2'] = 'bar2';
$a['foo3'] = 'bar3';
$a['foo4'] = 'bar4';

Is there some other syntax so I can add elements like this without overwriting

$a = [ 'foo2' => 'bar2',
'foo3' => 'bar3',
'foo4' => 'bar4'];

Answer Source

You can also do something like this:

$arr1 = array('foo2' => 'bar2');
$arr2 = array('foo3' => 'bar3');
$arr3 = $arr1 + $arr2;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download