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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.