Group dates in a foreach loop

前端 未结 3 700
日久生厌
日久生厌 2021-01-15 04:54

I want to add a new line in order to group dates within a foreach loop.

Example:

2015-11-05
2015-11-05

2015-11-07

2015-11-10
2015-11-1         


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-15 05:30

    One way to do this is to save the previous value and compare it to the current one:

    $prev = NULL;
    foreach($rows AS $row) {
        $curr = $row['datetime_published'];
        echo "$curr
    "; if ($curr != $prev) { echo '
    '; $prev = $curr; } }

提交回复
热议问题