Jump to content

Array help


esport

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.