Re: Random selection

From: Date: Sat, 23 Aug 2003 13:48:15 +0000
Subject: Re: Random selection
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-160501@lists.php.net to get a copy of this message
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.

« previous php.general (#160501) next »