如何从foreach循环的第二次迭代开始输出?

时间:2017-03-02 17:28:52

标签: foreach output iteration

我在对象的每次迭代中输出以下代码。如何从第二次迭代开始输出?

<?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;?>

1 个答案:

答案 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;?>

对于第二次迭代以及之后的情况都是如此。