RE: [PHP] multiple random items

From: Date: Mon, 16 Jul 2007 15:27:35 +0000
Subject: RE: [PHP] multiple random items
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-258938@lists.php.net to get a copy of this message
> -----Original Message----- > From: chris@aquanuke.com [mailto:chris@aquanuke.com] > Sent: 16 July 2007 15:40 > To: php-general@lists.php.net > Subject: [PHP] multiple random items > > > Hi I have this script which pulls 1 random item from a txt file. > How would I modify it to pull 10 random items. > > <?php > $delim = "\n"; > $quotefile = "names.txt"; > $fp = fopen($quotefile, "r"); > $contents = fread($fp, filesize($quotefile)); > $quote_arr = explode($delim,$contents); > fclose($fp); $random_keys = array_rand($quote_arr, min(count($quote_arr), 10)); foreach ($random_keys as $quote_index) { // get quote at $quote_index and return it $herequote = $quote_arr[$quote_index]; echo "<li><a href='http://www.$herequote'>$herequote</a></li>"; } ?> This won't produce any duplicates and will cope with any number of items. Edward

« previous php.general (#258938) next »