note 33237 deleted from function.array-merge by sniper
| From: | sniper@php.net | Date: | Mon, 06 Oct 2003 22:54:35 +0000 |
| Subject: | note 33237 deleted from function.array-merge by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57914@lists.php.net to get a copy of this message | ||
Note Submitter: dragon_rage4@hotmail.com
----
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
}