Click to See Complete Forum and Search --> : changing array order


Sid3335
07-24-2006, 10:46 AM
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

NogDog
07-24-2006, 11:29 AM
list($array[1], $array[2]) = array($array[2], $array[1]);

Sid3335
07-25-2006, 02:49 AM
so the array i have is:


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 ) )


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

i have this so far:

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 ;
}


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