note 87620 deleted from function.array-rand by danbrown

From: Date: Fri, 12 Dec 2008 23:34:46 +0000
Subject: note 87620 deleted from function.array-rand by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-148112@lists.php.net to get a copy of this message
Note Submitter: admin at jeremyzaretski dot com ---- Whoops... forgot the email in my last post... vista_ at live dot se: I didn't want to leave without trying to help. Perhaps a more foreach-friendly variant of array_rand() could be useful: function Select_Random_Indices($source_array, $count = 1) { if($count >= 0) { if($count == 1) { result = array(array_rand($source_array, $count)); } else { result = array_rand($source_array, $count); } } else { $result = array(); } return $result; } Then you could do something like this: function Select_Random_Entries($source_array, $count = 1) { $result = array(); $index_array = Select_Random_Indices($source_array, $count); foreach($index_array as $index) { $result[] = $source_array[$index]; } return $result; }

« previous php.notes (#148112) next »