DarkHylian Posted January 3, 2011 Share Posted January 3, 2011 Hi everyone Well I've started learning php yesterday and now I encountered my first problem, I looked around but I don't really know what to search for if I want my answer so here I come <html> <body> <?php $names = array('Peter','Quagmire','Joe'); $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); $who = rand(0,2); $who2 = $names[$who]; echo "$names[$who] is $ages[$who2] years old"; ?> </body> </html> This is the script I have, and there is the kind of script I expected: <html> <body> <?php $names = array('Peter','Quagmire','Joe'); $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); $who = rand('Peter','Quagmire','Joe'); echo "$who is $ages[$who] years old"; ?> </body> </html> But after some search I concluded that rand is only used for random numbers So, my question is, which function should I use to achieve my goal? Is there any randstring('string1','string2',etc) command? Tnx for your time Quote Link to comment https://forums.phpfreaks.com/topic/223229-random-string-noob-question-p/ Share on other sites More sharing options...
Zurev Posted January 3, 2011 Share Posted January 3, 2011 Look into array_rand. http://php.net/manual/en/function.array-rand.php Or you could always just pick the first value, and shuffle in between each, though that's probably less efficient. http://www.php.net/manual/en/function.shuffle.php Quote Link to comment https://forums.phpfreaks.com/topic/223229-random-string-noob-question-p/#findComment-1154059 Share on other sites More sharing options...
.josh Posted January 3, 2011 Share Posted January 3, 2011 $names = array_keys($ages); $who = rand(0,count($names)-1); $who = $ages[$names[$who]]; Quote Link to comment https://forums.phpfreaks.com/topic/223229-random-string-noob-question-p/#findComment-1154060 Share on other sites More sharing options...
DarkHylian Posted January 3, 2011 Author Share Posted January 3, 2011 Thank a lot for the quick answer! Quote Link to comment https://forums.phpfreaks.com/topic/223229-random-string-noob-question-p/#findComment-1154062 Share on other sites More sharing options...
JD* Posted January 3, 2011 Share Posted January 3, 2011 Hi, and welcome to programming in PHP. You're going to want to do something more like this: <html> <body> <?php $characters = array('Peter'=>32,'Quagmire'=>30,'Joe'=>34); $rand_character = array_rand($characters); echo "$rand_character is $characters[$rand_character] years old"; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/223229-random-string-noob-question-p/#findComment-1154063 Share on other sites More sharing options...
Anti-Moronic Posted January 3, 2011 Share Posted January 3, 2011 ..and for flexibility you might want to do it this way: $characters = array( array('name'=>'Peter', 'age'=>32), array('name'=>'Quagmire', 'age'=>30), array('name'=>'Joe', 'age'=>34) ); $who = $characters[rand(0, count($characters)-1)]; echo $who['name'] . ' is ' . $who['age'] . ' years old'; That way you can easily add new information to each character without conflicting with the key=>index structure of the array. Quote Link to comment https://forums.phpfreaks.com/topic/223229-random-string-noob-question-p/#findComment-1154074 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.