note 13909 added to function.array-rand.php

From: Date: Mon, 09 Jul 2001 22:09:37 +0000
Subject: note 13909 added to function.array-rand.php
Groups: php.notes 
Request: Send a blank email to php-notes+get-15135@lists.php.net to get a copy of this message
If you're just trying to draw a random subset of n elements from an array, it seems more effecient to do something like this: function draw_rand_array($array,$draws) { $lastIndex = count($array) - 1; $returnArr = array(); while($draws > 1) { $rndIndex = rand(0,$lastIndex); array_push($returnArr,array_splice($array,$rndIndex,1)); $draws--; $lastIndex--; } return $returnArr; } No messing with indexes when you're done... you just have an array with the elements you're looking for in it. http://www.php.net/manual/en/function.array-rand.php

« previous php.notes (#15135) next »