note 30655 deleted from ref.array by sniper
| From: | sniper@php.net | Date: | Sat, 18 Oct 2003 18:01:51 +0000 |
| Subject: | note 30655 deleted from ref.array by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58826@lists.php.net to get a copy of this message | ||
Note Submitter: rudecoder@yahoo.com
----
Here's a tested function that will help you swap array elements with minimal effort.
function reposition($myarray,$oldpos,$newpos){
$temparray = $myarray;
for($i=0; $i < count($myarray); $i++){
if($i == $oldpos){
$temparray[$newpos] = $myarray[$i];
}
if($i == $newpos){
$temparray[$oldpos] = $myarray[$i];
}
}
return $temparray;
}
// Example of use...
$most_popular = array("Seineld","Elaine","Kramer","George");
// Now let's make Elaine more popular then Kramer
$most_popular = reposition($most_popular,3,1);
//Now we have an array with Elaine and George in swapped positions:
//$most_popular[0] = "Seinfeld";
//$most_popular[1] = "George";
//$most_popular[2] = "Kramer";
//$most_popular[3] = "Elaine";