talk2toyin Posted March 13, 2013 Share Posted March 13, 2013 I need help returning 4 different values from and array containning mixed alphabets A to Z and a to z. My code looks something like this: <?php $mixedL = array ("A", "B", "C"......."Z", "a", "b", "c",....."Z" ); $randLet = rand($mixedL); return $randLet; ?> Quote Link to comment https://forums.phpfreaks.com/topic/275612-returning-random-values-from-an-array/ Share on other sites More sharing options...
yomanny Posted March 13, 2013 Share Posted March 13, 2013 Create a function called getRandomLetter() or something, which: 1. Generates a random number, rand($min, $max). 2. Returns the element in the array with the generated number, like: return $mixedL[$randomizednumber] Then call this function every time you need a new number. - W Quote Link to comment https://forums.phpfreaks.com/topic/275612-returning-random-values-from-an-array/#findComment-1418410 Share on other sites More sharing options...
Barand Posted March 13, 2013 Share Posted March 13, 2013 $mixedL = array_merge(range('A','Z'), range('a', 'z')); for ($i=0; $i<10; $i++) { shuffle($mixedL); echo join(array_slice($mixedL,0,4)) .'<br>'; } /*** results OPTq HSoh awfI CNKj fjHG Pmht ZOWs PzoJ pqYz FnfM */ or $mixedL = array_merge(range('A','Z'), range('a', 'z')); $x = array_rand($mixedL, 4); foreach ($x as $ind) { echo $mixedL[$ind]; } Quote Link to comment https://forums.phpfreaks.com/topic/275612-returning-random-values-from-an-array/#findComment-1418412 Share on other sites More sharing options...
talk2toyin Posted March 13, 2013 Author Share Posted March 13, 2013 A big thank you for your reply. I will try them out right now Quote Link to comment https://forums.phpfreaks.com/topic/275612-returning-random-values-from-an-array/#findComment-1418423 Share on other sites More sharing options...
talk2toyin Posted March 13, 2013 Author Share Posted March 13, 2013 Thanks Barand for your reply, it was helpful. The first works great if I was to specify the numbers of results to be generated with a form. The second on the other hand doesn't output anything...why is this? Quote Link to comment https://forums.phpfreaks.com/topic/275612-returning-random-values-from-an-array/#findComment-1418464 Share on other sites More sharing options...
talk2toyin Posted March 13, 2013 Author Share Posted March 13, 2013 Sorry about that Barand, my mistake. Both works perfectly...thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/275612-returning-random-values-from-an-array/#findComment-1418466 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.