Insert “element” among other “elements” of the array (loop) php

后端 未结 3 1056
遥遥无期
遥遥无期 2021-01-29 11:35

my code causes the images to appear randomly within the page. But how to insert an \"element\" (in my case would be a div) between these images?



        
3条回答
  •  遇见更好的自我
    2021-01-29 12:11

    You would want something like this...

     $myImagesList = array (
        'image1.png',
        'image2.png',
        'image3.png',
        'image4.png'
     );
     shuffle ($myImagesList);
    
     $i = 0;
     foreach ($myImagesList as $img) {
    
        $i++;
    
        if ($i % 3 === 0) { /* show content */ }
    
        echo '';
    
     }
    

    This will give you the content section every third iteration, no matter the size of the list.

提交回复
热议问题