簡體   English   中英

我如何只回顯 foreach 迭代一次

[英]how can I echo foreach iteration only once

是否可以只echo顯一次 foreach 的結果? 這就是我想要實現的目標:

18-sept-2016 | 08:00 AM | some text
               11:00 AM | some text
19-sept-2016 | 09:00 AM | some text
               09:10 AM | some text
               12:00 AM | some text

我的代碼

<?php if (!empty($notification)): ?>
            <table class="table" id="notification_table">
                <tbody>
                    <?php foreach ($notification as $row): ?>
                        <tr>
                            <td nowrap>
                                <?php echo date('d M',strtotime($row['created_on'])) ?>
                            </td>
                            <td><?php echo date('h:m A', strtotime($row['created_on'])) ?></td>
                            <td>
                                <?php echo $row['notification_text'] ?>
                            </td>
                        </tr>
                        <?php endforeach ?>
                </tbody>
            </table>
            <?php else: ?>
                <div class="alert alert-warning">
                    No notification
                </div>
            <?php endif ?>

我目前的結果:

18-sept-2016 | 08:00 AM | some text
18-sept-2016 | 11:00 AM | some text
19-sept-2016 | 09:00 AM | some text
19-sept-2016 | 09:10 AM | some text
19-sept-2016 | 12:00 AM | some text

如果我只echo顯一次日期,我認為它看起來比我當前的結果更有效,但我不知道該怎么做

簡單像這樣:

    $dates = [];
    foreach ($notification as $row)

         $date = date('d M',strtotime($row['created_on']));
         if( !in_array($dates, $date ) ){
             echo $date;
             $dates[] = $date;
         }
      .....
   }

檢查數據是否在日期數組中,如果沒有,則回顯並將其存儲在日期數組中以備下次使用。

另一種方法是將最后日期保存在臨時變量中,並將其與當前迭代日期進行比較:

<?php if (!empty($notification)):
$date = ''; ?>
<table class="table" id="notification_table">
    <tbody>
        <?php foreach ($notification as $row): ?>
        <tr>
            <td nowrap>
            <?php if($date != $row['created_on']){
                echo date('d M',strtotime($row['created_on']));
                $date = $row['created_on'];
            } ?>
            </td>
            <td><?php echo $row['notification_text'] ?></td>
        </tr>
        <?php endforeach ?>
    </tbody>
</table>
<?php else: ?>
    <div class="alert alert-warning">No notification</div>
<?php endif ?>

首先初始化一個像$is_same_date = ''這樣的$is_same_date = ''空白,我按照以下方式進行回答,因為您的數據是序列化的。

制作一個 if 條件來檢查您的數據是否已打印,如果它在$is_same_date則無需再次顯示日期,否則您必須顯示日期並將日期存儲到$is_same_date

<?php 
$your_date = date('d M',strtotime($row['created_on']));
if($is_same_date != '' && $is_same_date == $your_date)
    echo '';
else{
    echo $your_date;
    $is_same_date = $your_date;
}
?>

你可以通過這種方式獲得

          <tbody>
               <!-- define $previous_value variable here to keep tmp value -->
                <?php $previous_value = ""; ?>
                <?php foreach ($notification as $row): ?>
                    <tr>
                        <td nowrap>
                            <?php 
                            if($previous_value != $row['created_on']){
                            echo date('d M',strtotime($row['created_on']));
                            }
                            $previous_value = $row['created_on'];
                            ?>
                        </td>
                        <td><?php echo date('h:m A', strtotime($row['created_on'])) ?></td>
                        <td>
                            <?php echo $row['notification_text'] ?>
                        </td>
                    </tr>
                    <?php endforeach ?>
            </tbody>

如果日期未在輸入數組中排序,這將有所幫助,如下例所示:

<?php
$notification=array(0=>array('created_on'=> '18-sept-2016 08:00 AM','notification_text'=>'some text1'),
                    1=>array('created_on'=> '19-sept-2016 09:10 AM','notification_text'=>'some text2'),
                    2=>array('created_on'=> '19-sept-2016 09:00 AM','notification_text'=>'some text3'),
                    3=>array('created_on'=> '18-sept-2016 11:00 AM','notification_text'=>'some text4'),
                    4=>array('created_on'=> '19-sept-2016 12:00 AM','notification_text'=>'some text5'));

$new_array=array();
foreach($notification as $index => $value){
     $item=array();
     $item['time']=date('h:m A', strtotime($value['created_on'])); 
     $item['text']=$value['notification_text'];
     $new_array[date('d M',strtotime($value['created_on']))][]=$item;
}
?>
<?php if (!empty($new_array)): ?>
            <table class="table" id="notification_table">
                <tbody>
                    <?php foreach ($new_array as $index=>$row): ?>
                        <?php foreach ($row as $index_item=>$row_item): ?>
                        <tr>
                            <td nowrap>
                                <?php if($index_item ==0) echo $index ?>
                            </td>
                            <td><?php echo $row_item['time'] ?></td>
                            <td>
                                <?php echo $row_item['text'] ?>
                            </td>
                        </tr>
                          <?php endforeach ?>
                        <?php endforeach ?>
                </tbody>
            </table>
            <?php else: ?>
                <div class="alert alert-warning">
                    No notification
                </div>
            <?php endif ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM