Jump to content

[SOLVED] easy quick questions


ngreenwood6

Recommended Posts

First, I am new to arrays and am trying to figure them out.  Please let me know if this is the correct format or if there is a better way:

 


$array = ("first","second","third","fourth","fifth");

 

Second, I am trying to display one of these values randomly. I am using the following code but it is giving me this error "Warning: rand() expects exactly 2 parameters, 1 given in C:\wamp\www\random.php on line 5":

 


<?php

$array = ("first","second","third","fourth","fifth");

$random = rand($array);

echo $random;

?>

 

 

Any help is appreciated.

Link to comment
Share on other sites

That's not how array_rand works, and no offense, but if you even took a minute to read the manual page, which was posted in this thread already, you'd realize it returns a random array KEY.  So therefore, you'd do something like:

 

<?php
$input = array("first","second","third","fourth","fifth");
$rand_key = array_rand($input);
echo $input[$rand_key];
?>

Link to comment
Share on other sites

the code i gave you worked for me

 

<?php
$input = array("first","second","third","fourth","fifth");
$rand_keys = array_rand($input, 5);
echo $input[$rand_keys[0]]."<br>";
echo $input[$rand_keys[1]]."<br>";
echo $input[$rand_keys[2]]."<br>";
echo $input[$rand_keys[3]]."<br>";
echo $input[$rand_keys[4]]."<br>";
?>

 

You just had to change the vars do do what you wanted to do bro

Link to comment
Share on other sites

the code i gave you worked for me

 

<?php
$input = array("first","second","third","fourth","fifth");
$rand_keys = array_rand($input, 5);
echo $input[$rand_keys[0]]."<br>";
echo $input[$rand_keys[1]]."<br>";
echo $input[$rand_keys[2]]."<br>";
echo $input[$rand_keys[3]]."<br>";
echo $input[$rand_keys[4]]."<br>";
?>

 

You just had to change the vars do do what you wanted to do bro

 

1) It's impossible that it worked for you because you specified a 1 as the second parameter of array_rand, which tells it how many items to get random keys for, and when you put 1 (default anyway), it returns the key by itself and not an array.

 

2) Use <br /> and not <br>.

Link to comment
Share on other sites

the code i gave you worked for me

 

<?php
$input = array("first","second","third","fourth","fifth");
$rand_keys = array_rand($input, 5);
echo $input[$rand_keys[0]]."<br>";
echo $input[$rand_keys[1]]."<br>";
echo $input[$rand_keys[2]]."<br>";
echo $input[$rand_keys[3]]."<br>";
echo $input[$rand_keys[4]]."<br>";
?>

 

You just had to change the vars do do what you wanted to do bro

 

1) It's impossible that it worked for you because you specified a 1 as the second parameter of array_rand, which tells it how many items to get random keys for, and when you put 1 (default anyway), it returns the key by itself and not an array.

 

2) Use <br /> and not <br>.

 

Cmon bro, did you try it?

http://www.phpecono.com/array.php

 

Works like a charm. This is an exemple suggested by PHP.net

Link to comment
Share on other sites

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.