array_rand can be used to select a list of values randomly. This helped me to code a server side script to display a random grid of pictures as part of a project. These pictures changes every time the page is requested or refreshed.
Below is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php srand((float) microtime() * 10000000); $input = array("lion.gif", "cat.gif", "dog.gif", "bird.gif", "tiger.gif", "eagle.gif"); $rand_keys = array_rand($input, 4); ?> <table align="center" cellspacing="15"> <tr> <td bgcolor="#f0f0f0" align="middle"> <img src="<?php echo $input[$rand_keys[0]] ?>"> </td> <td bgcolor="#f0f0f0" align="middle"> <img src="<?php echo $input[$rand_keys[1]] ?>"> </tr> <tr> <td bgcolor="#f0f0f0" align="middle"> <img src="<?php echo $input[$rand_keys[2]] ?>"> </td> <td bgcolor="#f0f0f0" align="middle"> <img src="<?php echo $input[$rand_keys[3]] ?>"> </tr> </table> |
