我在对象的每次迭代中输出以下代码。如何从第二次迭代开始输出?
<?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;?>
答案 0 :(得分:0)
最简单的方法是将输出括在
中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;?>
对于第二次迭代以及之后的情况都是如此。