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
https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/
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];
?>

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

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

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

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.