note 56330 added to function.array-push

From: Date: Tue, 30 Aug 2005 21:47:51 +0000
Subject: note 56330 added to function.array-push
Groups: php.notes 
Request: Send a blank email to php-notes+get-94450@lists.php.net to get a copy of this message
regarding the speed of oneill's solution to insert a value into a non-associative array, I've done some tests and I found that it behaves well if you have a small array and more insertions, but for a huge array and a little insersions I sugest using this function: function array_insert( &$array, $index, $value ) { $cnt = count($array); for( $i = $cnt-1; $i >= $index; --$i ) { $array[ $i + 1 ] = $array[ $i ]; } $array[$index] = $value; } or if you are a speed adicted programmer (same situation: big array, few insertions) use this: array_splice ( $array, $offset, 0, $item ); item may also be an array of values ;). ---- Manual Page -- http://www.php.net/manual/en/function.array-push.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56330 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56330&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56330&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56330&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56330&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56330&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56330&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56330&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94450) next »