note 110847 added to function.array-merge
| From: | kokuswolf at pireality dot de | Date: | Fri, 14 Dec 2012 11:36:05 +0000 |
| Subject: | note 110847 added to function.array-merge | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-192529@lists.php.net to get a copy of this message | ||
@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 :)
----
Server IP: 213.172.110.14
Probable Submitter: 217.91.162.102
----
Manual Page -- http://php.net/manual/en/function.array-merge.php
Edit -- https://master.php.net/note/edit/110847
Del: integrated -- https://master.php.net/note/delete/110847/integrated
Del: useless -- https://master.php.net/note/delete/110847/useless
Del: bad code -- https://master.php.net/note/delete/110847/bad+code
Del: spam -- https://master.php.net/note/delete/110847/spam
Del: non-english -- https://master.php.net/note/delete/110847/non-english
Del: in docs -- https://master.php.net/note/delete/110847/in+docs
Del: other reasons-- https://master.php.net/note/delete/110847
Reject -- https://master.php.net/note/reject/110847
Search -- https://master.php.net/manage/user-notes.php