doubaben7394 2013-03-10 17:27
浏览 28
已采纳

PHP返回一个数组中的前三个元素然后另外三个[关闭]

I have an array of 20 elements and each element in the array holds an image. I'm making it so the image sizes alternate. Three images should be medium size, then next four are small, and 7th is large. Pattern should continue till end of the array. Right now I have it working so that every 7th image is large, and the rest are small. I'm not sure what the best way would be to set up the medium images. So: Array[]

[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
[11]
[12]
[13]
[14]
[15]
[16]
[17]
[18]
[19]
[20]

I would want [0], [1], [2], [8],[9],[10],[15],[16],[17] to all be medium sized.

Here's my code below.

        foreach ($images as $image ) {
                $img_size = "small";
                if($i !==0 && $i % 7 == 0) {
                    $img_size = "large";
                }else{
                    $img_size = $img_size;
                }
        }
  • 写回答

2条回答 默认 最新

  • dongyan1899 2013-03-10 18:57
    关注

    Some if/else logic:

    <?php
    
    $m = 3;
    $s = 7;
    $l = 8;
    
    $counter = 1;
    for($i=0;$i<=20;$i++){
        if($counter <= $m){
            $img_size = "medium";
        }elseif($counter <= $s){
            $img_size = "small";
        }else{
            $img_size = "large";
        }
        if($counter == $l) $counter = 0;
    
        $counter++;
        echo "i = $i and img_size = $img_size <br>";
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成