2016-03-07 38 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

相关问题