note 13386 deleted from function.shuffle by philip

From: Date: Wed, 03 Apr 2002 22:34:48 +0000
Subject: note 13386 deleted from function.shuffle by philip
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-28577@lists.php.net to get a copy of this message
Regarding to the assotiative arrays. Indeed shuffle() will destroy your keys. The way around it may be done by using $Arr2Shuffle = array(); $Arr2Shuffle["one"]="1"; // populate it: $Arr2Shuffle["two"]="2"; $Arr2Shuffle["three"]="3"; $ArrKeys = array(); while ( list( $key, $value ) = each ($Arr2Shuffle) ) { $ArrKeys[] = $key; // save keys in the array } srand((double)microtime()*1000000); // shuffle keys ... shuffle($ArrKeys); srand((double)microtime()*1000000); // twice to 2 b sure (thanks 2 joel@cringe.com ) shuffle($ArrKeys); for ($i=0; $i<count($ArrKeys); $i++ ) { // display results $k = $ArrKeys[$i]; print $Arr2Shuffle[$k]."<br>"; }

« previous php.notes (#28577) next »