note 35060 added to function.array-splice
| From: | php-general at lists dot php dot net | Date: | Mon, 18 Aug 2003 08:57:32 +0000 |
| Subject: | note 35060 added to function.array-splice | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-54566@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.
$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.
----
Manual Page -- http://www.php.net/manual/en/function.array-splice.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35060
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35060&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35060&report=yes