note 121466 deleted from function.array-merge by salathe

From: Date: Mon, 06 Sep 2021 22:42:27 +0000
Subject: note 121466 deleted from function.array-merge by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-216874@lists.php.net to get a copy of this message
Note Submitter: titum at lavache dot com ---- foreach loop is faster than array_merge to append values to an existing array, so choose the loop instead if you want to add an array to the end of another. <?php // Create an array of arrays $chars = []; for ($i = 0; $i < 15000; $i++) { $chars[] = array_fill(0, 10, 'a'); } // test array_merge $new = []; $start = microtime(TRUE); foreach ($chars as $splitArray) { $new = array_merge($new, $splitArray); } echo microtime(true) - $start; // => 14.61776 sec // test foreach $new = []; $start = microtime(TRUE); foreach ($chars as $splitArray) { foreach ($splitArray as $value) { $new[] = $value; } } echo microtime(true) - $start; // => 0.00900101 sec // ==> 1600 times faster ?>

« previous php.notes (#216874) next »