esport Posted October 7, 2007 Share Posted October 7, 2007 Hey Guys, I have and array of dates for example: $calDAtes = array(20071001,20071002,20071003,20071004,20071006,20071008,20071009,20071010,20071011); Now I want to create another array that stores the first and last date that are consecutive. So the above example will be: $dateArray[0]['firstDate'] = 20071001 , $dateArray[0]['lasttDate'] = 20071004 $dateArray[1]['firstDate'] = 20071006 ,$dateArray[1]['lasttDate'] = 20071006 $dateArray[2]['firstDate'] = 20071008 ,$dateArray[2]['lasttDate'] = 200710011 Any help would be much appreciated. Daniel Link to comment https://forums.phpfreaks.com/topic/72153-array-help/ Share on other sites More sharing options...
BlueSkyIS Posted October 7, 2007 Share Posted October 7, 2007 well, in case the dates aren't sorted, sort them: $calDAtes = sort($calDAtes); Then pair each... pair, something like this: $a_count = 0; for ($i=0;$i<count($calDAtes);$i+=2) { $dateArray[$a_count]['firstDate'] = $calDAtes[$i]; $dateArray[$a_count]['firstDate'] = $calDAtes[$i + 1]; $a_count++; } Link to comment https://forums.phpfreaks.com/topic/72153-array-help/#findComment-363801 Share on other sites More sharing options...
esport Posted October 7, 2007 Author Share Posted October 7, 2007 Thanks for you response, however your only pairing every second entry. I only want to pair the first date and last date that are in a sequence. Link to comment https://forums.phpfreaks.com/topic/72153-array-help/#findComment-363833 Share on other sites More sharing options...
Barand Posted October 7, 2007 Share Posted October 7, 2007 try <?php $calDates = array(20071001,20071002,20071003,20071004,20071006,20071008,20071009,20071010,20071011); $result = array(); $j=0; for ($i=0, $k=count($calDates); $i<$k-1; $i++) { if (!isset($result[$j][0]))$result[$j] = array($calDates[$i], $calDates[$i]); if ($calDates[$i] == date('Ymd', strtotime("-1 day {$calDates[$i+1]}"))) { $result[$j][1] = $calDates[$i+1]; } else { $j++; } } --> Array ( [0] => Array ( [0] => 20071001 [1] => 20071004 ) [1] => Array ( [0] => 20071006 [1] => 20071006 ) [2] => Array ( [0] => 20071008 [1] => 20071011 ) ) Link to comment https://forums.phpfreaks.com/topic/72153-array-help/#findComment-363914 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.