note 30655 added to ref.array

From: Date: Tue, 25 Mar 2003 03:52:11 +0000
Subject: note 30655 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-45778@lists.php.net to get a copy of this message
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"; -- http://www.php.net/manual/en/ref.array.php http://master.php.net/manage/user-notes.php?action=edit+30655 http://master.php.net/manage/user-notes.php?action=delete+30655 http://master.php.net/manage/user-notes.php?action=reject+30655

« previous php.notes (#45778) next »