note 98386 added to function.array-merge

From: Date: Fri, 11 Jun 2010 18:46:24 +0000
Subject: note 98386 added to function.array-merge
Groups: php.notes 
Request: Send a blank email to php-notes+get-170073@lists.php.net to get a copy of this message
I had a hard time using array_merge with large datasets. By the time my web framework was in memory there wasn't enough space to have multiple copies of my dataset. To fix this I had to remove any functions which operated on the data set and made a copy in memory (pass by value). I realize the available memory to an application instance is modifiable, but I didn't think I should have to set it below the default 16mb for a web app! Unfortunately, a number of php functions pass by value internally, so I had to write my own merge function. This passes by reference, utilizes a fast while loop (thus doesn't need to call count() to get an upper boundary, also a php pass by value culprit), and unsets the copy array (freeing memory as it goes). <?php function mergeArrays(&$sourceArray, &$copyArray){ //merge copy array into source array $i = 0; while (isset($copyArray[$i])){ $sourceArray[] = $copyArray[$i]; unset($copyArray[$i]); $i++; } } ?> This fixed the problem. I would love to know if there is an even faster, more efficient way. Simply adding the two arrays won't work since there is an overlap of index. ---- Server IP: 69.147.83.197 Probable Submitter: 38.108.250.130 ---- Manual Page -- http://www.php.net/manual/en/function.array-merge.php Edit -- https://master.php.net/note/edit/98386 Del: integrated -- https://master.php.net/note/delete/98386/integrated Del: useless -- https://master.php.net/note/delete/98386/useless Del: bad code -- https://master.php.net/note/delete/98386/bad+code Del: spam -- https://master.php.net/note/delete/98386/spam Del: non-english -- https://master.php.net/note/delete/98386/non-english Del: in docs -- https://master.php.net/note/delete/98386/in+docs Del: other reasons-- https://master.php.net/note/delete/98386 Reject -- https://master.php.net/note/reject/98386 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#170073) next »