Seaholme Posted August 17, 2010 Share Posted August 17, 2010 Hey, This is my [faulty] PHP $input3 = array("BB, BB, BB, BB, BB, BB, BB, BB, BB, bb"); $rand_keys3 = array_rand($input3, 2); $BB1 = $input3[$rand_keys3[0]] . "\n"; $BB2 = $input3[$rand_keys3[1]] . "\n"; $input4 = array("HD, HD, HD, HD, HD, hd"); $rand_keys4 = array_rand($input4, 2); $HD1 = $input4[$rand_keys4[0]] . "\n"; $HD2 = $input4[$rand_keys4[1]] . "\n"; $input5 = array("DD, DD, DD, DD, DD, DD, DD, dd"); $rand_keys5 = array_rand($input5, 2); $D1 = $input5[$rand_keys5[0]] . "\n"; $D2 = $input5[$rand_keys5[1]] . "\n"; $input6 = array("CC, CC, CC, CC, CC, cc"); $rand_keys6 = array_rand($input6, 2); $C1 = $input6[$rand_keys6[0]] . "\n"; $C2 = $input6[$rand_keys6[1]] . "\n"; $input7 = array("HG, HG, HG, HG, HG, HG, HG, HG, HG, HG, HG, HG, HG, HG, HG, HG, HG, HG, HG, hg"); $rand_keys7 = array_rand($input7, 2); $HG1 = $input7[$rand_keys7[0]] . "\n"; $HG2 = $input7[$rand_keys7[1]] . "\n"; And this is the error I'm getting: Warning: array_rand() [function.array-rand]: Second argument has to be between 1 and the number of elements in the array in /home/content/91/6351791/html/dogtrader.php on line 48 Warning: array_rand() [function.array-rand]: Second argument has to be between 1 and the number of elements in the array in /home/content/91/6351791/html/dogtrader.php on line 54 Warning: array_rand() [function.array-rand]: Second argument has to be between 1 and the number of elements in the array in /home/content/91/6351791/html/dogtrader.php on line 60 Warning: array_rand() [function.array-rand]: Second argument has to be between 1 and the number of elements in the array in /home/content/91/6351791/html/dogtrader.php on line 66 Warning: array_rand() [function.array-rand]: Second argument has to be between 1 and the number of elements in the array in /home/content/91/6351791/html/dogtrader.php on line 72 I've checked back over it all and looked up the function in various places to check what they say about it, but can't see which bit I'm going wrong wit! :[ If anybody has any advice as to what to do to fix it, it would be much appreciated. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/210954-help-with-a-rand-function/ Share on other sites More sharing options...
bh Posted August 17, 2010 Share Posted August 17, 2010 Hi, your array has only one element: $input3 = array("BB, BB, BB, BB, BB, BB, BB, BB, BB, bb"); -> $input3 = array("BB", "BB", "BB", ..., "bb"); Quote Link to comment https://forums.phpfreaks.com/topic/210954-help-with-a-rand-function/#findComment-1100303 Share on other sites More sharing options...
Seaholme Posted August 17, 2010 Author Share Posted August 17, 2010 I'm afraid I'm not really knowledgeable enough to know what you mean by that. Do you mean that I should change it from for instance: $input4 = array("HD, HD, HD, HD, HD, hd"); $rand_keys4 = array_rand($input4, 2); $HD1 = $input4[$rand_keys4[0]] . "\n"; $HD2 = $input4[$rand_keys4[1]] . "\n"; to $input4 = array("HD, HD, HD, HD, HD, hd"); $rand_keys4 = array_rand($input4); $HD1 = $input4[$rand_keys4[0]] . "\n"; $HD2 = $input4[$rand_keys4[1]] . "\n"; ?? I don't quite follow the significance of the "...."! Thanks so much. Quote Link to comment https://forums.phpfreaks.com/topic/210954-help-with-a-rand-function/#findComment-1100311 Share on other sites More sharing options...
bh Posted August 17, 2010 Share Posted August 17, 2010 Dont you see the difference the two lines? Look it again. it is one array element, cuz its a string "BB, BB, BB" this line is two array elements "BB", "BB" array("BB, BB, BB"); -> one element array("BB", "BB", "BB"); -> three elements Here array_rand($input4, 2); $input4 is an array which has one element and you want to choose randomly two... Quote Link to comment https://forums.phpfreaks.com/topic/210954-help-with-a-rand-function/#findComment-1100315 Share on other sites More sharing options...
JasonLewis Posted August 17, 2010 Share Posted August 17, 2010 You're not creating your arrays correctly, if you do print_r on your $input4 you'll get an output of: Array ( [0] => HD, HD, HD, HD, HD, hd ) As you can see, you only have 1 element in your array, this is because you haven't separated them by commas correctly. However, if you change it too: $input4 = array("HD", "HD", "HD", "HD", "HD", "hd"); It will output as: Array ( [0] => HD [1] => HD [2] => HD [3] => HD [4] => HD [5] => hd ) Can you see the difference? (I'll post this anyway. ) Quote Link to comment https://forums.phpfreaks.com/topic/210954-help-with-a-rand-function/#findComment-1100317 Share on other sites More sharing options...
bh Posted August 17, 2010 Share Posted August 17, 2010 [off] we did everything, let him away [/off] sorry for the off Quote Link to comment https://forums.phpfreaks.com/topic/210954-help-with-a-rand-function/#findComment-1100323 Share on other sites More sharing options...
Seaholme Posted August 17, 2010 Author Share Posted August 17, 2010 Oooh, gotcha! Clearly wasn't looking carefully enough there! Awesome, thank you both so much for the very clear explanations, sorry for being a bit slow! Quote Link to comment https://forums.phpfreaks.com/topic/210954-help-with-a-rand-function/#findComment-1100343 Share on other sites More sharing options...
JasonLewis Posted August 17, 2010 Share Posted August 17, 2010 Oooh, gotcha! Clearly wasn't looking carefully enough there! Awesome, thank you both so much for the very clear explanations, sorry for being a bit slow! Don't worry about, you're learning. I remember when I was afraid of arrays. Quote Link to comment https://forums.phpfreaks.com/topic/210954-help-with-a-rand-function/#findComment-1100344 Share on other sites More sharing options...
bh Posted August 17, 2010 Share Posted August 17, 2010 Yeah, just up with the head. Quote Link to comment https://forums.phpfreaks.com/topic/210954-help-with-a-rand-function/#findComment-1100346 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.