I've noticed recently in PHP you can do this.
$myNewArray = $oldArray + $someArray;
This looks completely different to anything I've seen before involving manipulating arrays in PHP.
How and why does it work? Are there any pitfalls?
I have recently started using it in some places where I may have used array_unshift()
and array_merge()
.
When in doubt, consult the documentation. The behavior is different from array_merge: array_merge appends/overwrites, +
only appends.
Example:
<?php
$a = Array('foo'=>'bar','baz'=>'quux');
$b = Array('foo'=>'something else','xyzzy'=>'aaaa');
$c = $a + $b;
$d = array_merge($a,$b);
print_r($c);
print_r($d);
Output - as you see, array_merge overwrote the value from $a['foo'] with $b['foo']; $a+$b did not:
Array
(
[foo] => bar
[baz] => quux
[xyzzy] => aaaa
)
Array
(
[foo] => something else
[baz] => quux
[xyzzy] => aaaa
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With