note 45984 deleted from function.array-merge by bjori

From: Date: Fri, 31 Jan 2014 17:04:23 +0000
Subject: note 45984 deleted from function.array-merge by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-198377@lists.php.net to get a copy of this message
Note Submitter: nospam at veganismus dot ch ---- I needed to add some values (array) to an array on each loop. I used merge because it seemed to be the right way. But array_splice() ist much faster: <? $merge = array(); $splice = array(); function makearray() { //to do on each loop //return two values: return array('foo','bar'); } for($i=0;$i<1000;$i++) $merge = array_merge($merge,makearray()); echo count($merge); //returns 2000 after about 2 sec (PHP5) for($i=0;$i<1000;$i++) array_splice($splice, 0,0,makearray()); echo count($splice); //returns 2000 after about 0.8 sec (PHP5) ?> Note: The sorting will be diffrent but you can use array_splice($splice, count($splice),0,makearray()); which is still faster (1.4 sec)

« previous php.notes (#198377) next »