note 33237 added to function.array-merge
| From: | dragon_rage4 at hotmail dot com | Date: | Thu, 19 Jun 2003 19:34:34 +0000 |
| Subject: | note 33237 added to function.array-merge | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-50678@lists.php.net to get a copy of this message | ||
A problem I ran into was confusion between array_merge and array_push. array_push works on all
variable types except arrays themselves. In those cases, array_merge is the road to follow. Example:
# The following does not work because
# you cannot push an array onto another
$myFirstArray = array("a", "b");
$mySecondArray = array("c", "d");
array_push($myFirstArray, $mySecondArray); // This pushes "Array" onto $myFirstArray, not
"cd"
for ($i = 0; $i < count($myFirstArray); $i++)
{
print $myFirstArray[$i]; // This will print abArray
}
# However, array_merge() does the trick
$myThirdArray = array("a", "b");
$myFourthArray = array("c", "d");
$myThirdArray = array_merge($myThirdArray, $myFourthArray); // Merges two arrays into a single array
for ($i = 0; $i < count($myThirdArray); $i++)
{
print $myThirdArray[$i]; // This will print abcd
}
----
Manual Page -- http://www.php.net/manual/en/function.array-merge.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33237
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33237&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33237&report=yes