Posted By

gfazioli on 01/27/09


Tagged

php random shufflearrayslice implode


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

gfazioli
umang_nine


Get random item array in PHP


 / Published in: PHP
 

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

  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>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: alexy on February 17, 2010

Or... $a=array("Hello", "test", "test");$a=array_shuffle($a);echo $a[0];

You need to login to post a comment.