note 105761 added to function.array-merge

From: Date: Tue, 13 Sep 2011 02:03:31 +0000
Subject: note 105761 added to function.array-merge
Groups: php.notes 
Request: Send a blank email to php-notes+get-183129@lists.php.net to get a copy of this message
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. ---- Server IP: 187.73.33.5 Probable Submitter: 186.204.71.66 ---- Manual Page -- http://www.php.net/manual/en/function.array-merge.php Edit -- https://master.php.net/note/edit/105761 Del: integrated -- https://master.php.net/note/delete/105761/integrated Del: useless -- https://master.php.net/note/delete/105761/useless Del: bad code -- https://master.php.net/note/delete/105761/bad+code Del: spam -- https://master.php.net/note/delete/105761/spam Del: non-english -- https://master.php.net/note/delete/105761/non-english Del: in docs -- https://master.php.net/note/delete/105761/in+docs Del: other reasons-- https://master.php.net/note/delete/105761 Reject -- https://master.php.net/note/reject/105761 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#183129) next »