IMB IMB - 1 month ago 6
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
?

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

Answer

You can also do something like this:

$arr1 = array('foo2' => 'bar2');
$arr2 = array('foo3' => 'bar3');
$arr3 = $arr1 + $arr2;