note 87657 modified in function.array-rand by danbrown
| From: | danbrown@php.net | Date: | Mon, 15 Dec 2008 16:12:48 +0000 |
| Subject: | note 87657 modified in function.array-rand by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-148176@lists.php.net to get a copy of this message | ||
<?php
// a foreach friendly version of array_rand
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;
}
// using the above function to pick random values instead of entries
function Select_Random_Entries($source_array, $count = 1)
{
$result = array();
$index_array = Select_Random_Indices($source_array, $count);
foreach($index_array as $index)
{
$result[$index] = $source_array[$index];
}
return $result;
}
?>
--was--
// a foreach friendly version of array_rand
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;
}
// using the above function to pick random values instead of entries
function Select_Random_Entries($source_array, $count = 1)
{
$result = array();
$index_array = Select_Random_Indices($source_array, $count);
foreach($index_array as $index)
{
$result[$index] = $source_array[$index];
}
return $result;
}
http://php.net/manual/en/function.array-rand.php