Array Logic
| From: | Steve Keller | Date: | Mon, 19 May 2003 17:50:31 +0000 |
| Subject: | Array Logic | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-148186@lists.php.net to get a copy of this message | ||
Perhaps there's a function for this, but I've been unable to find it, and none of the key word searches I've done on the archive have turned up the response I'm looking for.
The question is this, what's the easiest way to add an element to the middle of an array with numeric keys and shift all of the other elements down one?
For example, I create an array called $a
Array
(
[0] => lime
[1] => coconut
[2] => rum
[3] => ice
)
And I want to add "seltzer" as element 2 and shift the others down 1. So now my array looks like:
Array
(
[0] => lime
[1] => coconut
[2] => seltzer
[3] => rum
[4] => ice
)
I've looked, but couldn't find a function to do this. The only way I've come up with to do it is with a loop.
$fillElement = 2;
for ($i=$count($myArray); $i>$fillElement; $i--) {
$myArray[$i] = $myArray[$i-1];
}
$myArray[$fillElement] = "seltzer";
Anyone got a better idea?
--
S. Keller
UI Engineer
The Health TV Channel, Inc.
(a non - profit organization)
3820 Lake Otis Pkwy.
Anchorage, AK 99508
907.770.6200 ext.220
907.336.6205 (fax)
Email: skeller@healthtvchannel.org
Web: www.healthtvchannel.org