Get random item array in PHP


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <ul>
  2. <?php
  3. // LI items
  4. $a = array(
  5. "<li>Elemento 1</li>",
  6. "<li>Elemento 2</li>",
  7. "<li>Elemento 3</li>",
  8. "<li>Elemento 4</li>",
  9. "<li>Elemento 5</li>",
  10. "<li>Elemento 6</li>",
  11. "<li>Elemento 7</li>",
  12. "<li>Elemento 8</li>",
  13. "<li>Elemento 9</li>"
  14. );
  15. shuffle( $a );
  16. $na = array_slice( $a, 0, 4 );
  17. echo implode('', $na );
  18. ?>
  19. </ul>

URL: http://www.undolog.com/2009/01/27/very-short-trick-prendere-elementi-casuali-da-un-array-in-php/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.