在循环中,我希望每次能够做 第一次有新的一天。
例如:
<div>31/12/1999</div>
<div>31/12/1999</div>
<div>1/01/2000</div> //do something to this row
<div>1/01/2000</div>
<div>1/01/2000</div>
<div>2/01/2000</div> //do something to this row
<div>2/01/2000</div>
我想要这样的东西,但我无法弄明白。
while($date = mysql_fetch_array($dates)){
if($current_date != $previous_date){
//do stuff
}
}
答案 0 :(得分:1)
相当简单,php从顶部到底部读取,这样你就可以利用它。
//Setting the variable so we don't get UNDEFINED INDEX on the first run
$previous_date = '';
while($date = mysql_fetch_array($dates)){
$current_date = $date;
if($current_date != $previous_date){
//do stuff
}
$previous_date = $current_date;
}
这样$previous_date
将包含最后一个值,同时在比较完成后用新日期覆盖。
答案 1 :(得分:0)
每当第一次出现日期时,我们都可以将其存储在名为$ unique_dates的变量中。
试试这个:
$unique_dates = [];
$dates = mysql_fetch_array($dates);
//$dates = ["31/12/1999", "31/12/1999", "1/01/2000", "1/01/2000", "1/01/2000", "2/01/2000", "2/01/2000"];
foreach($dates as $k => $date) {
if (!in_array($date, $unique_dates)) {
$unique_dates[] = $date;
echo $date." has appeared for the first time in ".$k." index";
echo "<br/>";
}
}