note 26902 added to function.array-rand
| From: | seaATuniversalmetropolis dot com at rack1 dot php dot net | Date: | Sat, 16 Nov 2002 23:11:10 +0000 |
| Subject: | note 26902 added to function.array-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-39580@lists.php.net to get a copy of this message | ||
Posting this again, don't know where the original wound up -
A much simpler way to pick a random entry out of an array.
// place your required values in an array
$colours =
array("green","red","orange","blue","peach");
// pick a random value from the array (minus one because your range in the array starts at 0 rather
than 1)
$colours = $colours[rand(0,count($colours)-1)];
// verify
print $colours;
Remember to seed the random generator with srand() at the beginning of your script.
--
http://www.php.net/manual/en/function.array-rand.php
http://master.php.net/manage/user-notes.php?action=edit+26902
http://master.php.net/manage/user-notes.php?action=delete+26902
http://master.php.net/manage/user-notes.php?action=reject+26902