may
may

Reputation: 755

how can I echo foreach iteration only once

Is it possible to echo the result of foreach only once? This is what I want to achieve:

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

My code

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

My current result :

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

I think it looks more efficient than my current result if I echo the date only once , but I don't know how to do it

Upvotes: 1

Views: 824

Answers (5)

Amani Ben Azzouz
Amani Ben Azzouz

Reputation: 2565

This will help in case that dates are not sorted in the input array as shown in this exapmle:

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

Upvotes: 0

Janaka
Janaka

Reputation: 408

You can get it in this way

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

Upvotes: 0

Murad Hasan
Murad Hasan

Reputation: 9583

First initialize a varialble blank like $is_same_date = '', I make the answer in the following manner cause your data is serialize.

Make a if condition to check your data is already printed or not, if it is in the $is_same_date then no need to show the date again, else you have to show the date and store your date to $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;
}
?>

Upvotes: 0

d.coder
d.coder

Reputation: 2038

Another way to do this is hold last date in a temp variable and compare it with current date of iteration:

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

Upvotes: 0

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21661

Simple Like this:

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

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

Check if a data is in the dates array, if not, echo and store it in the dates array for next time.

Upvotes: 1

Related Questions