note 105761 deleted from function.array-merge by danbrown
| From: | danbrown@php.net | Date: | Tue, 13 Sep 2011 14:26:15 +0000 |
| Subject: | note 105761 deleted from function.array-merge by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-183146@lists.php.net to get a copy of this message | ||
Note Submitter: heitor dot siller at gmail dot com
----
This is a less memory intensive algorithm version of (jpakin at gmail dot com 11-Jun-2010
03:46)'s function:
<?php
function mergeArrays(&$sourceArray, &$copyArray, $copyArrayName){
//merge copy array into source array
$i = 0;
while (isset($copyArray[$i])){
$sourceArray[] = $copyArray[$i];
// OLD way (doesn't work as expected):
//unset($copyArray[$i]);
// CORRECT way:
unset($GLOBALS[ $copyArrayName ][ $i ]);
$i++;
}
}
?>
The main difference is that unset() doesn't really free memory of variables inside a function,
even if they are passed by reference. To mitigate it, the $GLOBALS variable must be used instead,
while the $copyArray variable name must be informed in order to use $GLOBALS. And, of course, the
$copyArray must be global.