note 92602 added to function.array-merge
| From: | JulianEgelstaff at osu1 dot php dot net | Date: | Fri, 31 Jul 2009 06:48:54 +0000 |
| Subject: | note 92602 added to function.array-merge | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158937@lists.php.net to get a copy of this message | ||
In some situations, the union operator ( + ) might be more useful to you than array_merge. The
array_merge function does not preserve numeric key values. If you need to preserve the numeric
keys, then using + will do that.
ie:
<?php
$array1[0] = "zero";
$array1[1] = "one";
$array2[1] = "one";
$array2[2] = "two";
$array2[3] = "three";
$array3 = $array1 + $array2;
//This will result in::
$array3 = array(0=>"zero", 1=>"one", 2=>"two",
3=>"three");
?>
Note the implicit "array_unique" that gets applied as well. In some situations where your
numeric keys matter, this behaviour could be useful, and better than array_merge.
--Julian
----
Server IP: 209.41.74.194
Probable Submitter: 99.231.215.159
----
Manual Page -- http://www.php.net/manual/en/function.array-merge.php
Edit -- https://master.php.net/note/edit/92602
Del: integrated -- https://master.php.net/note/delete/92602/integrated
Del: useless -- https://master.php.net/note/delete/92602/useless
Del: bad code -- https://master.php.net/note/delete/92602/bad+code
Del: spam -- https://master.php.net/note/delete/92602/spam
Del: non-english -- https://master.php.net/note/delete/92602/non-english
Del: in docs -- https://master.php.net/note/delete/92602/in+docs
Del: other reasons-- https://master.php.net/note/delete/92602
Reject -- https://master.php.net/note/reject/92602
Search -- https://master.php.net/manage/user-notes.php