note 35060 modified in function.array-splice by vrana

From: Date: Tue, 17 Aug 2004 14:49:45 +0000
Subject: note 35060 modified in function.array-splice by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79939@lists.php.net to get a copy of this message
Want to insert a new value in the middle of the array, without overwriting other elements? Try this. <?php $array = array( 0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5 ); array_splice($array, 3, count($array), array_merge(array('x'), array_slice($array, 3))); echo '<pre>'; print_r($array); ?> Output: Array ( [0] => 0 [1] => 1 [2] => 2 [3] => x [4] => 3 [5] => 4 [6] => 5 ) As you can see, the operation add 'X' in the 4th place, pushing everything else to the next key. --was-- Want to insert a new value in the middle of the array, without overwriting other elements? Try this. $array = array( 0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5 ); array_splice($array, 3, count($array), array_merge(array('x'), array_slice($array, 3))); echo '<pre>'; print_r($array); Output: Array ( [0] => 0 [1] => 1 [2] => 2 [3] => x [4] => 3 [5] => 4 [6] => 5 ) As you can see, the operation add 'X' in the 4th place, pushing everything else to the next key. http://php.net/manual/en/function.array-splice.php

« previous php.notes (#79939) next »