/    Sign up×
Community /Pin to ProfileBookmark

changing array order

How can i move an element in an array up one index place (moving the above element down)

for example:
Andrew
David
William
Dierdre

how can i move William above David, is there a function or will i have to use a temp variable to copy over and replace

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 24.2006 — [code=php]
list($array[1], $array[2]) = array($array[2], $array[1]);
[/code]
Copy linkTweet thisAlerts:
@Sid3335authorJul 25.2006 — so the array i have is:

[code=php]
Array (
[0] => Array ( [COLUMN_NAME] => name [DATA_TYPE] => varchar )
[1] => Array ( [COLUMN_NAME] => alive [DATA_TYPE] => tinyint )
[2] => Array ( [COLUMN_NAME] => age [DATA_TYPE] => int ) )
[/code]


i'm trying to move the array elements around either up one index or down one index.

i have this so far:
[code=php]
switch ($order)
{
case 'up' :
list($array2[$index], $array2[$index - 1]) = array($array2[$index - 1], $array2[$index]);
$array_to_swap = $index - 1;
echo "<br><br>attempt swap: array element " . $index . " with array element " . $array_to_swap . " <br><br>" ;
break ;
case 'down' :
list($array2[$index], $array2[$index + 1]) = array($array2[$index + 1], $array2[$index]);
$array_to_swap = $index + 1;
echo "<br><br>attempt swap: array element " . $index . " with array element " . $array_to_swap . " <br><br>" ;
break ;
}
[/code]


but it doesn't work in any sensical manner. does it matter that its a multi dimensional array?
×

Success!

Help @Sid3335 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...