Re: Random selection

From: Date: Sat, 23 Aug 2003 13:53:53 +0000
Subject: Re: Random selection
Groups: php.general 
Request: Send a blank email to php-general+get-160503@lists.php.net to get a copy of this message
Hi, Thanks for the answer.. I'm open for faster ways!!! >BTW, if a value appears multiple times in a database column then your >database may be a good candidate for normalisation. Problem i'm selecting some holidays out of an database but I want to avoid that there are holiday's presented from the same place.. THanks, Frank Sorry but i'm trying to avoid >On Sat, 23 Aug 2003 15:06:32 +0200 (CEST), you wrote: > >>In my mysql db i have a colum called names; >> >>In names their are: >> >>Frank >>Frank >>Bob >>Alice >>Bob >>Alice >>Jim >>Alice >>Frank >> >>I want to make a random selection (max 3 value's for example).. Only it may not produce >>two times the same name. For example; > >First make sure the array values are distinct: > >$array = array_unique ($array); > >Then shuffle the array: > >shuffle ($array); > >Then slice the array down to size: > >if (sizeof ($array) > 3) >{ > $array = array_slice ($array, 0, 3); >} > >There are faster ways, but this is probably the shortest code snippet. > >BTW, if a value appears multiple times in a database column then your >database may be a good candidate for normalisation. > > >-- >PHP General Mailing List (http://www.php.net/) >To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.general (#160503) next »