reddog Posted September 22, 2011 Share Posted September 22, 2011 Hello all, I am newbie to php programming, any suggestions or advance greatly appreciated. I can't figure out how to output two array random numbers in ascending order. <?php $l_numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47); $mega = range(1, 27); for($i = 0; $i <= 4; $i++) { $random1 = array_rand($l_numbers); echo "$random1 \n" ; } for($e = 1; $e <= 1; $e++) { $random2 = array_rand($mega); echo "<p> Mega number is $random2 </p>"; } echo "Winning numbers are $random1 and mega $random2"; ?> example: Winning numbers are 3,5,10,17,39 and mega 5. Quote Link to comment https://forums.phpfreaks.com/topic/247631-help-with-random-array-numbers/ Share on other sites More sharing options...
priti Posted September 22, 2011 Share Posted September 22, 2011 Can you please let us know what is the expected output Quote Link to comment https://forums.phpfreaks.com/topic/247631-help-with-random-array-numbers/#findComment-1271648 Share on other sites More sharing options...
reddog Posted September 22, 2011 Author Share Posted September 22, 2011 The output I am looking for is: 5 random numbers from first array and 1 random number from second array in ascending order. Example: The winning numbers are 4,7,20,27, 31 and Mega number is 4. Quote Link to comment https://forums.phpfreaks.com/topic/247631-help-with-random-array-numbers/#findComment-1271649 Share on other sites More sharing options...
cyberRobot Posted September 22, 2011 Share Posted September 22, 2011 Have you looked into array_rand(): http://php.net/manual/en/function.array-rand.php For the sorting part, you could look into sort(): http://us3.php.net/manual/en/function.sort.php Quote Link to comment https://forums.phpfreaks.com/topic/247631-help-with-random-array-numbers/#findComment-1271684 Share on other sites More sharing options...
cyberRobot Posted September 22, 2011 Share Posted September 22, 2011 Sorry, just noticed that you're already using array_rand(). You could simplify the code by doing something like: <?php //... $random1 = array_rand($l_numbers, 5); sort($random1); echo implode(', ', $random1); //... ?> Quote Link to comment https://forums.phpfreaks.com/topic/247631-help-with-random-array-numbers/#findComment-1271687 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.