note 110847 deleted from function.array-merge by danbrown
| From: | danbrown@php.net | Date: | Thu, 17 Jan 2013 15:36:41 +0000 |
| Subject: | note 110847 deleted from function.array-merge by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193165@lists.php.net to get a copy of this message | ||
Note Submitter: kokuswolf at pireality dot de
----
@izhar_aazmi at gmail dot com
@ventzy dot public at gmail dot com
... and for every other. If array_merge() isn't the resolution for your problem, please take a
look the array operator + (UNION):
<?php
$a = ['test'=>1, (object)['name'=>'object 1 at index 0'],
'foo stays at index 1'];
$b = [(object)['name'=>'object 2 at index 0'], 'test'=>2];
var_dump($b + $a); // Same results as the foreach() example
// array(3) {
// [0]=> object(stdClass)#1 (1) {
// ["name"]=> string(19) "object 2 at index 0"
// }
// ["test"]=> int(2)
// [1]=> string(20) "foo stays at index 1"
// }
?>
The key here is to use $b + $a, not $a + $b, since the keys of the first array are locked for
overwriting, or in other words: $first + $second means copying any new keys (not value) in $second
to $first. This relates to any keytype, doesn't have any limitations to the type of value and
no value gets an different index!!!
PS: On older PHP-Version use array() instead of [] in my example :)