note 55790 modified in function.array-splice by thiago

From: Date: Thu, 24 Jul 2008 17:21:43 +0000
Subject: note 55790 modified in function.array-splice by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142670@lists.php.net to get a copy of this message
Appending arrays If you have an array $a2 whose values you would like to append to an array $a1 then four methods you could use are listed below in order of increasing time. The last two methods took significantly more time than the first two. The most surprising lesson is that using the & incurs a time hit. <?php foreach ($a2 as $elem) $a1[]=$elem; foreach ($a2 as &$elem) $a1[]=$elem; array_splice ($a1, count($a1), 0, $a2); $a1 = array_merge($a1, $a2); ?> Csaba Gabor from Vienna --was-- Appending arrays If you have an array $a2 whose values you would like to append to an array $a1 then four methods you could use are listed below in order of increasing time. The last two methods took significantly more time than the first two. The most surprising lesson is that using the & incurs a time hit. foreach ($a2 as $elem) $a1[]=$elem; foreach ($a2 as &$elem) $a1[]=$elem; array_splice ($a1, count($a1), 0, $a2); $a1 = array_merge($a1, $a2); Csaba Gabor from Vienna http://php.net/manual/en/function.array-splice.php

« previous php.notes (#142670) next »