我有一個包含圖像的陣列。我已經寫了一個循環遍歷這個數組並顯示每個圖像的函數。但出於某種原因,數組中的第一個圖像不會顯示在頁面上。嘗試顯示圖像時丟失了陣列中的第一張照片
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)
你每次只用一個=來代替$輸出,而不是把它與a連接在一起。= –
不,不是。因爲它必須被覆蓋,而它是一個string_replace –
如果你分享你的print_r($ fotos)或var_dump($ fotos),那麼我會告訴你。我認爲問題出在您的foreach聲明中。如果你有一個普通的數組而不需要使用$ key = $ foto。 – DevMan