note 87620 added to function.array-rand
| From: | admin at jeremyzaretski dot com | Date: | Fri, 12 Dec 2008 23:27:02 +0000 |
| Subject: | note 87620 added to function.array-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-148110@lists.php.net to get a copy of this message | ||
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;
}
----
Server IP: 69.147.83.197
Probable Submitter: 206.75.15.5
----
Manual Page -- http://www.php.net/manual/en/function.array-rand.php
Edit -- https://master.php.net/note/edit/87620
Del: integrated -- https://master.php.net/note/delete/87620/integrated
Del: useless -- https://master.php.net/note/delete/87620/useless
Del: bad code -- https://master.php.net/note/delete/87620/bad+code
Del: spam -- https://master.php.net/note/delete/87620/spam
Del: non-english -- https://master.php.net/note/delete/87620/non-english
Del: in docs -- https://master.php.net/note/delete/87620/in+docs
Del: other reasons-- https://master.php.net/note/delete/87620
Reject -- https://master.php.net/note/reject/87620
Search -- https://master.php.net/manage/user-notes.php