RE: [PHP] Array Logic
| From: | Ford, Mike [LSS] | Date: | Tue, 20 May 2003 08:31:54 +0000 |
| Subject: | RE: [PHP] Array Logic | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-148266@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Steve Keller [mailto:skeller@healthtvchannel.org]
> Sent: 19 May 2003 18:51
>
>
> 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.
Try array_splice():
array_splice($a, 2, 0, "seltzer");
should do what you want.
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211