1

我试过下面的代码

<?php

foreach ($_wishlistitemCollection as $_wishlistitem):
    $_product  = $_wishlistitem->getProduct();
    $imgpath   = $this->helper('catalog/image')->init($_product, 'small_image');
    $physpaths = array($imgpath);
endforeach;
?>
<?php

for ($i = 0; $i < 5; $i++) {
    echo $physpaths[$i];
}
?> 

没有错误,但问题是它没有显示 array 中的所有元素$physpaths

请帮助我如何确保$physpaths包含所有元素或简单地指出我犯了错误的地方。

4

2 回答 2

4

很近 ;)

<?php
$physpaths = array();
foreach ($_wishlistitemCollection as $_wishlistitem):
    $_product  = $_wishlistitem->getProduct();
    $imgpath   = $this->helper('catalog/image')->init($_product, 'small_image');
    $physpaths[] = $imgpath;
endforeach;
?>
<?php

for ($i = 0; $i < 5; $i++) {
    echo $physpaths[$i];
}
?> 
于 2012-08-23T14:45:08.440 回答
1

为了在新索引处将值附加到数组,您应该使用array_push()[]

<?php
foreach(...) :
    .
    .
    .
    // use this
    $physpaths[] = $imgpath;
    // or this
    array_push($physpaths, $imgpath);
    // NOT BOTH
endforeach;

然后,不要循环遍历每个数组索引并使用echo,只需使用var_dump()

echo "<pre>";
var_dump($physpaths);
echo "</pre>";

提示:您应该$physpathsforeach.

$physpaths = array();

foreach(...):
    .
    .
    .
endforeach;
于 2012-08-23T14:45:01.967 回答