ngreenwood6 Posted September 3, 2008 Share Posted September 3, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/ Share on other sites More sharing options...
drisate Posted September 3, 2008 Share Posted September 3, 2008 use this http://ca3.php.net/array_rand Quote Link to comment https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/#findComment-633035 Share on other sites More sharing options...
ngreenwood6 Posted September 3, 2008 Author Share Posted September 3, 2008 Its not giving me the error now but it is just giving me numbers instead of the actual values. Any help? Quote Link to comment https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/#findComment-633036 Share on other sites More sharing options...
drisate Posted September 3, 2008 Share Posted September 3, 2008 If you can, i would recommend using this select * from yourtable order by rand() limit 0,1; A lot faster Quote Link to comment https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/#findComment-633040 Share on other sites More sharing options...
drisate Posted September 3, 2008 Share Posted September 3, 2008 By the way, use the numbers it returns in your var like this to get the value echo $array[$num]; Quote Link to comment https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/#findComment-633042 Share on other sites More sharing options...
ngreenwood6 Posted September 3, 2008 Author Share Posted September 3, 2008 Yeah the only problem with that is that the data is not held in a table for this specific instance. Any clues as to why it is showing the numbers instead of the actual values? Quote Link to comment https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/#findComment-633043 Share on other sites More sharing options...
ngreenwood6 Posted September 3, 2008 Author Share Posted September 3, 2008 sorry posted that before I got to read yours. The point of this is to get a random value and if I do the $array['1'] it is always going to give me the first one instead of a random one. I want it to be random. Quote Link to comment https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/#findComment-633046 Share on other sites More sharing options...
adam291086 Posted September 3, 2008 Share Posted September 3, 2008 whats your current code? Quote Link to comment https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/#findComment-633048 Share on other sites More sharing options...
drisate Posted September 3, 2008 Share Posted September 3, 2008 <?php $input = array("first","second","third","fourth","fifth"); $rand_keys = array_rand($input, 1); echo $input[$rand_keys[0]]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/#findComment-633049 Share on other sites More sharing options...
ngreenwood6 Posted September 3, 2008 Author Share Posted September 3, 2008 dristate the code that you just gave me does not output anything on the page. my current code was at the top of the page. Quote Link to comment https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/#findComment-633052 Share on other sites More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 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]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/#findComment-633054 Share on other sites More sharing options...
ngreenwood6 Posted September 3, 2008 Author Share Posted September 3, 2008 Darkwater that worked like a charm. Thanks for all of your help. Sorry I am not good with reading how stuff works better at looking at examples and modifying for my needs. Quote Link to comment https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/#findComment-633056 Share on other sites More sharing options...
drisate Posted September 3, 2008 Share Posted September 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/#findComment-633058 Share on other sites More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 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>. Quote Link to comment https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/#findComment-633063 Share on other sites More sharing options...
drisate Posted September 3, 2008 Share Posted September 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/#findComment-633071 Share on other sites More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 I'm talking about the example you gave him originally. You did the first one incorrectly. Go try your original code. Quote Link to comment https://forums.phpfreaks.com/topic/122600-solved-easy-quick-questions/#findComment-633073 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.