Moving elements of an array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cheerio
Forum Newbie
Posts: 18
Joined: Sat Aug 13, 2005 4:52 pm

Moving elements of an array?

Post by cheerio »

how do i move an element of an array to a different location? for example,

i have an array array(1,2,3,4,5,6)
and i want to move the 3 after the for so it ends up as array(1,2,4,3,5,6)

how is this done???
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

array_splice()

You'll need two of them. One to extract, one to insert.
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

simple manual solution

Code: Select all

$x = $aray[4];
$aray[4] = $aray[3];
$aray[3] = $x;
cheerio
Forum Newbie
Posts: 18
Joined: Sat Aug 13, 2005 4:52 pm

Post by cheerio »

thanks guys :)
Post Reply