note 13386 added to function.shuffle.php
| From: | jacob at spiralsolutions dot com | Date: | Thu, 14 Jun 2001 09:30:57 +0000 |
| Subject: | note 13386 added to function.shuffle.php | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-14393@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>";
}
http://www.php.net/manual/en/function.shuffle.php