Q695 Posted April 1, 2009 Share Posted April 1, 2009 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 More sharing options...
genericnumber1 Posted April 1, 2009 Share Posted April 1, 2009 $creatures will only be a single element long. What is $rowc['id']? If it's an array, you might mean to do... $creatures = $rowc[id]; Link to comment https://forums.phpfreaks.com/topic/152027-solved-random-array-item-chooser/#findComment-798400 Share on other sites More sharing options...
Q695 Posted April 1, 2009 Author Share Posted April 1, 2009 Now I get "First argument has to be an array". I would need it to choose a particular random generator algorithm if it's an array otherwise it does this one. Link to comment https://forums.phpfreaks.com/topic/152027-solved-random-array-item-chooser/#findComment-798408 Share on other sites More sharing options...
JasonLewis Posted April 1, 2009 Share Posted April 1, 2009 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. Link to comment https://forums.phpfreaks.com/topic/152027-solved-random-array-item-chooser/#findComment-798413 Share on other sites More sharing options...
Q695 Posted April 1, 2009 Author Share Posted April 1, 2009 Thanks Fear, I also noticed that it was randomly ordering the creatures. Link to comment https://forums.phpfreaks.com/topic/152027-solved-random-array-item-chooser/#findComment-798415 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.