2016-03-07 34 views
-1

我有一個包含圖像的陣列。我已經寫了一個循環遍歷這個數組並顯示每個圖像的函數。但出於某種原因,數組中的第一個圖像不會顯示在頁面上。嘗試顯示圖像時丟失了陣列中的第一張照片

function replacePhotoFlags($auto, $output) { 
    $fotos = explode("," , $auto['fotos']); 
    $count = 0; 
    print_r($fotos); 
    foreach($fotos as $key=>$foto){ 
     $img_url = FOTO_URL . '/' . get_option("ac") . '/foto/' . $foto; 
     if(@file_get_contents($img_url, 0, null, 0, 1)) { 
      $fotoURL = '<a class="fancybox" rel="group" href="' . $img_url . '"><img src="' . $img_url . '" /></a>'; 
      $output = str_replace("<<-- foto_medium_lightbox" . $key . " -->>", $fotoURL, $output); 
      $count++; 
     } else { 
      $output = str_replace("<<-- foto_medium_lightbox" . $key . " -->>", "", $output); 
     } 

    } 

    for($i = $count; $i <= 24; $i++) { 
     $output = str_replace("<<-- foto_medium_lightbox" . $i - 1 . " -->>", "", $output); 
    } 

    return $output; 
} 

print_r($ fotos)顯示第一張照片,但在foreach中缺失。 任何想法爲什麼它不顯示?

的print_r:

Array ([0] => 133494746501.JPG [1] => 133494746502.JPG [2] => 133494746503.JPG [3] => 1334947465039.JPG [4] => 133494746504.JPG [5] => 133494746505.JPG [6] => 133494746506.JPG [7] => 133494746507.JPG [8] => 133494746508.JPG [9] => 133494746509.JPG [10] => 133494746510.JPG [11] => 133494746511.JPG [12] => 133494746512.JPG [13] => 1334947465320.JPG [14] => 1334947465368.JPG [15] => 1334947465458.JPG [16] => 1334947465622.JPG [17] => 1334947465867.JPG [18] => 1334947465872.JPG [19] => 1334947465985.JPG) 
+0

你每次只用一個=來代替$輸出,而不是把它與a連接在一起。= –

+0

不,不是。因爲它必須被覆蓋,而它是一個string_replace –

+0

如果你分享你的print_r($ fotos)或var_dump($ fotos),那麼我會告訴你。我認爲問題出在您的foreach聲明中。如果你有一個普通的數組而不需要使用$ key = $ foto。 – DevMan

回答

0

也許標誌,你的鑰匙嘗試更換犯規存在,因爲在0密鑰開始和你的標誌1.

<<-- foto_medium_lightbox0 -->> 
+0

你是對的。這是問題所在。謝謝!! –

1

更換

for($i = $count; $i <= 24; $i++) { 
    $output = str_replace("<<-- foto_medium_lightbox" . $i - 1 . " -->>", "", $output); 
} 

隨着

$count=0; 
for($i = $count; $i <= 24; $i++) { 
    $output = str_replace("<<-- foto_medium_lightbox" . $i - 1 . " -->>", "", $output); 
} 
+0

爲什麼?已經這樣做 –

+1

,因爲在if語句中,你可以計算++,所以這個值變成1,這就是爲什麼你幾乎不會得到第一個圖像 –

+0

很好的發現 - @WouterdenOuden他的合作在你的if子句的最後一行,你正在做一個'$ count ++;',這意味着在你的for循環中'$ i'將從1開始或更高。 – Epodax

相關問題