Jump to content

[SOLVED] random array item chooser


Q695

Recommended Posts

I'm recieving the following error when choosing a random element from the following code:

Warning: array_rand() [function.array-rand]: Second argument has to be between 1 and the number of elements in the array

$creatures=array($rowc[id]);
$creature_num=mysql_num_rows($resultc);

$creature=array_rand($creatures, $creature_num);

How would I solve this issue?

Link to comment
https://forums.phpfreaks.com/topic/152027-solved-random-array-item-chooser/
Share on other sites

Maybe you want to do this, just guessing though since I haven't seen any other code.

 

while($r = mysql_fetch_array($resultc)){
$creatures[] = $r['id'];
}

$creature = array_rand($creatures, 1);

 

But as genericnumber1 said, your $creatures array will only contain 1 element.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.