Random image display

后端 未结 2 893
抹茶落季
抹茶落季 2021-01-29 04:29

I have a list of images that need to be displayed. However theres only space for 5. I need to display 5 of these at a time randomly.

Whats the best way to do this?

相关标签:
2条回答
  • 2021-01-29 04:51

    If you have the list of images in an array, you could use shuffle().

    0 讨论(0)
  • 2021-01-29 05:12

    There are plenty of ways to achive this.

    One example lets say you have all the image names in an array then you could:

    $images = array('imgAbc.jpg', 'img123.jpg'.... 'imgXYZ.gif');
    
    //randomize the array
    shuffle($images);
    
    //then just get the 5 elements you want:
    array_slice($images, 4);
    

    Other way could be if your images have numbers you could just generate 5 random numbers, etc. etc.

    As I said there are several ways to do it.

    0 讨论(0)
提交回复
热议问题