Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/210954-help-with-a-rand-function/
Share on other sites

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.

 

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...

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. :))

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.