PHP while 循环排除数组的第一个

发布于 2024-11-13 09:52:55 字数 795 浏览 2 评论 0原文

<div id="prettyphoto" align="left"> <a href="http://images.idealer1.com/getimage/999/<?php echo $image['vpid'] ?>.jpg" rel="prettyPhoto[gallery2]"><img src="http://images.idealer1.com/getimage/700/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" /></a>
  <ul class="gallery clearfix">
    <?php foreach ($images as $image) { ?>
    <li><a href="http://images.idealer1.com/getimage/999/<?php echo $image['vpid'] ?>.jpg" rel="prettyPhoto[gallery2]"><img src="http://images.idealer1.com/getimage/100/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" /></a></li>
    <? } ?>
  </ul>
</div>

我正在尝试采用缩略图的 foreach 循环并排除第一张图像并将其作为更大的图像加载,我似乎无法找出完成此操作的正确方法。

<div id="prettyphoto" align="left"> <a href="http://images.idealer1.com/getimage/999/<?php echo $image['vpid'] ?>.jpg" rel="prettyPhoto[gallery2]"><img src="http://images.idealer1.com/getimage/700/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" /></a>
  <ul class="gallery clearfix">
    <?php foreach ($images as $image) { ?>
    <li><a href="http://images.idealer1.com/getimage/999/<?php echo $image['vpid'] ?>.jpg" rel="prettyPhoto[gallery2]"><img src="http://images.idealer1.com/getimage/100/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" /></a></li>
    <? } ?>
  </ul>
</div>

I am trying to take this foreach loop of thumbnails and exclude the first image and have it load as a larger image I can't seem to figure out the proper way to accomplish this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅唱ヾ落雨殇 2024-11-20 09:52:55

您可以使用 array_shift()< /strong>

<?php $firstImage = array_shift($images); ?>
<!-- do something with $firstImage -->
<?php foreach ($images as $image): ?>
    ...
<?php endforeach; ?>

或者如果您不需要对第一张图像的引用,array_slice()

<?php foreach(array_slice($images, 1) as $image): ?>
    ...
<?php endforeach; ?>

还要注意 控制结构的替代语法,这使得阅读 PHP 和 HTML 的混合内容变得更容易(但不相关)到你的问题)。

You could use array_shift():

<?php $firstImage = array_shift($images); ?>
<!-- do something with $firstImage -->
<?php foreach ($images as $image): ?>
    ...
<?php endforeach; ?>

or if you don't need a reference to the first image, array_slice():

<?php foreach(array_slice($images, 1) as $image): ?>
    ...
<?php endforeach; ?>

Also note the use of the alternative syntax for control structures which makes reading the mixture of PHP and HTML a bit easier (but is not related to your problem).

对不⑦ 2024-11-20 09:52:55

您可以使用 next()、current() 等“手动”迭代数组:

<?php
$images = array(
    array('vpid' => 1, ),
    array('vpid' => 2, ),
    array('vpid' => 3, ),
    array('vpid' => 4, ),
);

$image = current($images);
?>
<div id="prettyphoto" align="left"> <a href="http://images.idealer1.com/getimage/999/<?php echo $image['vpid'] ?>.jpg" rel="prettyPhoto[gallery2]"><img src="http://images.idealer1.com/getimage/700/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" /></a>
  <ul class="gallery clearfix">
    <?php
    next($images);
    while(list($idx, $image) = each($images)) { ?>
    <li><a href="http://images.idealer1.com/getimage/999/<?php echo $image['vpid'] ?>.jpg" rel="prettyPhoto[gallery2]"><img src="http://images.idealer1.com/getimage/100/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" /></a></li>
    <? } ?>
  </ul>
</div>

阅读: http://www.php.net/manual/en/function.each.php

使用 array_shift() 你可以“松开”第一个图像(你必须在你完毕)。

使用 array_slice() 会重复数据,这通常是不好的做法。

You can iterate the array "manually", with next(), current(), etc:

<?php
$images = array(
    array('vpid' => 1, ),
    array('vpid' => 2, ),
    array('vpid' => 3, ),
    array('vpid' => 4, ),
);

$image = current($images);
?>
<div id="prettyphoto" align="left"> <a href="http://images.idealer1.com/getimage/999/<?php echo $image['vpid'] ?>.jpg" rel="prettyPhoto[gallery2]"><img src="http://images.idealer1.com/getimage/700/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" /></a>
  <ul class="gallery clearfix">
    <?php
    next($images);
    while(list($idx, $image) = each($images)) { ?>
    <li><a href="http://images.idealer1.com/getimage/999/<?php echo $image['vpid'] ?>.jpg" rel="prettyPhoto[gallery2]"><img src="http://images.idealer1.com/getimage/100/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" /></a></li>
    <? } ?>
  </ul>
</div>

Read: http://www.php.net/manual/en/function.each.php

With array_shift() you could "loose" the first image (you have to put it back in after you're done).

With array_slice() you would duplicate data, which is bad practice in general.

忆依然 2024-11-20 09:52:55

你可以使用一些想法:

foreach($array as $key=>$value)
{
    if($key==0)
    echo "code for the large image";
    else
    echo "code for the thumbnail image";
}

you can use some idea from this:

foreach($array as $key=>$value)
{
    if($key==0)
    echo "code for the large image";
    else
    echo "code for the thumbnail image";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文