如何从foreach循环的第二次迭代开始输出?
问题描述:
我有下面的代码输出每个迭代的对象。如何使输出从第二次迭代开始?如何从foreach循环的第二次迭代开始输出?
<?php foreach ($data['restos'] as $lid => $resto):?>
<?php
$time_arr = sic_get_start_expire_date_for_user_resto($this->current_user->ID, $lid);
$is_expired_class = '';
if (isset($time_arr['expire_time']) && time()>strtotime($time_arr['expire_time'])){
$is_expired_class = 'sic-expired-resto';
}
?>
<?php if (!empty($data['restos_metas']['sic_restos_on']) && !empty($level['resto_image_url'])):?>
<div class="resto-outer-wrapper <?php echo $is_expired_class;?>"><img src="<?php echo $resto['resto_image_url'];?>" class="resto" title="<?php echo $resto['label'];?>" /></div>
<?php elseif (!empty($resto['label'])):?>
<div class="sic-above <?php echo $is_expired_class;?>"><?php echo $resto['label'];?></div>
<?php endif;?>
<?php endforeach;?>
答
最简单的方法是将封闭输出
if ($uniqueVar) {
// OUTPUT
} else {
$uniquVar = true;
}
这对于你的代码看起来像:
<?php foreach ($data['restos'] as $lid => $resto):?>
<?php
$time_arr = sic_get_start_expire_date_for_user_resto($this->current_user->ID, $lid);
$is_expired_class = '';
if (isset($time_arr['expire_time']) && time()>strtotime($time_arr['expire_time'])){
$is_expired_class = 'sic-expired-resto';
}
?>
//----------------------------------
<?php if ($notFirstIteration):?>
//----------------------------------
<?php if (!empty($data['restos_metas']['sic_restos_on']) && !empty($level['resto_image_url'])):?>
<div class="resto-outer-wrapper <?php echo $is_expired_class;?>">
<img src="<?php echo $resto['resto_image_url'];?>" class="resto" title="<?php echo $resto['label'];?>" />
</div>
<?php elseif (!empty($resto['label'])):?>
<div class="sic-above <?php echo $is_expired_class;?>"><?php echo $resto['label'];?>
</div>
<?php endif;?>
//--------------------------------
<?php else:?>
<?php $notFirstIteration = true;?>
<?php endif;?>
//--------------------------------
<?php endforeach;?>
这将是真正的第二次迭代及以后。
除了缺少php标签,我认为你是对的。 – cErApHiM
@cErApHiM我明白你的意思,谢谢你建议编辑。我看过去了! – DallasO