By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,276 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,276 IT Pros & Developers. It's quick & easy.

How do I retrieve items from an array in random order?

P: 1
Hi. Can you help me please?
Is there any way to show a unique random item from an array, every time you refresh the page? In other words how do I show the items from an array in random order, one by one, by reloading the page?
Moreover the page have to be refreshed by pressing the "submit" button...

Thats what I achieved so far :) but it doesn't display the UNIQUE array item every time the page is reloaded...
as you may guess :)
Expand|Select|Wrap|Line Numbers
  1.  <html>
  2. <head><title></title></head>
  3. <body>
  4. <?php
  5. $testArray = array ("one","two","three","four","five");
  6. $number = rand(0,4);
  7. echo "$testArray[$number]";
  8. ?>
  9. <form method="post" action="test.php">
  10. <input type="submit" value="Submit">
  11. </form>
  12. </body>
  13. </html>

I'm a designer/ActionScript sort of a guy and know nothing about php,
so please don't laugh. :)
Any help is appreciated

May 10 '10 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 5K+
P: 8,639
it works as expected (at least for me), although you have to bear in mind that one out of five doesn’t give you a good distribution. it scrambled a bit better when using a larger base:
Expand|Select|Wrap|Line Numbers
  1. $number = rand(0, 100) % 5;
May 10 '10 #2

Expert 5K+
P: 6,050
array_rand() could maybe... possibly... work for you? ;)
May 10 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.