|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2002-08-20 17:57 UTC] edink@php.net
[2002-08-20 22:54 UTC] 007 at pathcom dot com
[2002-08-20 22:57 UTC] kalowsky@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 08:00:02 2025 UTC |
PHP installed with php-4.2.2-installer.exe. The code in the manual produced a set of random values that did not change on reloading the page in IE 5.01. If however the "num_required" argument was changed in the code, a new set of random values was generated. The following code with my own "array_random()" function works perfectly. I will use this code until I see the fix for array_rand(). <html> <head> <title>Example</title> </head> <body> <pre> <?php $Array = array(array("http://www.yahoo.com","yahoo.gif","Yahoo"), array("http://www.internet.com","internet.com.gif","Internet.com"), array("http://www.cnn.com","cnn.gif","CNN"), array("http://www.php.net","php.gif","PHP"), array("http://www.google.com","google.gif","Google"), ); print_r($Array); ?> </pre> <?php #srand ((float) microtime() * 10000000); # This seeds the random number generator. function array_random($array_name,$random_elements){ global $random_key; # This is the $random_key array. $x=count($array_name)-1; # 0 to $x ($x is the number of elements in the $array_name array) is the range of the random number we will generate for each element in the $random_key array. for ($i=0;$i<$random_elements;$i++){# First we give to each element in the $random_key array a random but not necessarily unique value. $random_key[$i]=rand(0,$x); } while (count(array_unique($random_key))<count($random_key)){ # Then we check the $random_key array values for uniqueness, and keep generating random values for each index until they're all unique. for ($i=0;$i<$random_elements;$i++){ $random_key[$i]=rand(0,$x); } } print_r($random_key); print "<br>"; } array_random($Array,2); # The first argument is the array form which we want to generate x (the second argument) number of random elements; x must set to 1 or more. print "\$random_key[1] value is : $random_key[1]"; ?> <table> <tr> <td><a href="<?=$Array[$random_key[0]][0]?>"><img src="images/<?=$Array[$random_key[0]][1]?>"></a></td><td><a href="<?=$Array[$random_key[1]][0]?>"><img src="images/<?=$Array[$random_key[1]][1]?>"></a></td> </tr> <tr> <td><?=$Array[$random_key[0]][2]?></td><td><?=$Array[$random_key[1]][2]?></td> </tr> </table> </body> </html>