Php – Array, how to display the “changing values” only

foreachPHP

Let's say I have an array with dates and seasons, there is one entry for each day. I would like to print the array row only when the season value is changing.

the array look like that:

2009-10-28 00:00:00 (good season)  

2009-10-29 00:00:00 (good season) 

2009-10-30 00:00:00 (good season) 

2009-10-31 00:00:00 (good season) 

2009-11-01 00:00:00 (good season) 

2009-11-02 00:00:00 (bad season) 

2009-11-03 00:00:00 (bad season) 

2009-11-04 00:00:00 (bad season) 

2009-11-05 00:00:00 (bad season) 

Best Answer

Loop through, keeping a record of the last season:

$lastSeason = '';

foreach ($array as $date => $season)
{
    if ($season != $lastSeason)
        echo "Season changed on " . $date;

    $lastSeason = $season;
}