尝试显示图像时,缺少阵列中的第一张照片

时间:2016-03-07 10:14:27

标签: php arrays

我有一个带图像的数组。我编写了一个循环遍历此数组并显示每个图像的函数。但由于某种原因,数组中的第一个图像不会显示在页面上。

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 ) 

2 个答案:

答案 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);
}

答案 1 :(得分:0)

也许你的密钥试图替换的标志不存在,因为你的密钥从0开始,你的标志在1。

<<-- foto_medium_lightbox0 -->>