array_rand behavior
| From: | Andrew Kirilenko | Date: | Sun, 24 Jun 2001 17:55:04 +0000 |
| Subject: | array_rand behavior | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-57853@lists.php.net to get a copy of this message | ||
Hello!
I was a little confused, while using array_rand first time...
Please, take a look at the following code:
--->
define("NUM", 2);
$keys = array_rand(array(0,1,2,3,4,5), NUM);
for ($i=0; $i < NUM; $i++)
echo $i ." - " . $keys[$i] . "<br>";
<---
If NUM is greater then 1, everything is OK, but if it's equal to 1,
array_rand return not array, but a scalar! And it's necessary to write
special code, like the following:
--->
if (NUM == 1)
$keys[0] = array_rand(array(0,1,2,3,4,5), NUM); // or $keys[0] = rand(0,
count(array(0,1,2,3,4,5)) - 1);
else
$keys = array_rand(array(0,1,2,3,4,5), NUM);
<---
Any suggestions, how to make array_rand more flexible?
Best regards,
Andrew Kirilenko,
Seniour Programmer / System Administrator,
Internet Service.