2013-03-10 47 views
-3

我有一個20個元素的數組,數組中的每個元素都包含一個圖像。我是這樣做的,因此圖像大小交替。三張圖像應該是中等大小,接下來的四張很小,第七張很大。模式應該繼續,直到數組結束。現在我有它的工作,使每第七個圖像很大,其餘的很小。我不確定設置介質圖像的最佳方式是什麼。所以: 數組[]PHP返回數組中的前三個元素,然後是另外三個

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

我想[0],[1],[2],[8],[9],[10],[15],[16],[17 ]都是中型的。

下面是我的代碼如下。

 foreach ($images as $image) { 
       $img_size = "small"; 
       if($i !==0 && $i % 7 == 0) { 
        $img_size = "large"; 
       }else{ 
        $img_size = $img_size; 
       } 
     } 
+3

酷,你嘗試過什麼?向我們展示您的代碼無法使用。 – sobol6803 2013-03-10 17:29:06

+1

很酷的項目,但是有什麼問題? – 2013-03-10 17:32:08

+0

更新後的帖子更多的信息。希望現在更清楚。 – user2154299 2013-03-10 18:40:10

回答

0

一些的if/else邏輯:

<?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

將if語句添加到我的原始foreach循環中,並且工作得很好。謝謝! – user2154299 2013-03-10 19:26:03

0

試試這個:

$i = 0; 
$count = count($a)-1; // get the maximum index of array $a 

while ($i <= $count) { 

    echo $a[i]; 
    if ($i+1 <= $count) echo $a[$i+1]; 
    if ($i+2 <= $count) echo $a[$i+2]; 

    $i+=8; 
} // while 
相關問題