Re: Re: randomize?

From: Date: Fri, 10 May 2002 13:52:03 +0000
Subject: Re: Re: randomize?
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-96951@lists.php.net to get a copy of this message
[..] >>> is there any way to limit to 4 in each group without 47839 rows of >>> code? [..] >> yes. you can. ;) [..] > oh, thank you very much :P [..] This might be a bit more helpful ;) There are several way to do it, this is just one. Unoptimised, aimed at readability. <?php // Split $items equally among $items function randGroup($items, $groups) { // Init $return = array(); $cnt = 0; // Shuffle srand ((float)microtime()*1000000); shuffle ($items); // Divide up while (sizeof($items)) { $return[$cnt++] = array_pop($items); if ($cnt >= $groups) $cnt = 0; } // Return return $return; } // Replace these with the items you want, and specifiy 4 groups $items = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14); $result = randGroup($items, 4); echo "<pre>"; print_r($result); echo "</pre>"; ?> -- Dan Hardiker [dhardiker@staff.firstcreative.net] ADAM Software & Systems Engineer

« previous php.general (#96951) next »