简体   繁体   中英

Split an array in sub arrays of consecutive dates

For a rent application, I have an array $dates like this:

Array
(
    [2013-07-19] => 1
    [2013-07-21] => 3
    [2013-07-23] => 2
    [2013-07-24] => 4
    [2013-07-25] => 4
    [2013-07-26] => 2
    [2013-07-27] => 2
    [2013-07-30] => 3
    [2013-07-31] => 1
)

The date is the key, and the values are the number of items rent in that day for a specific product

How can I split this array in many sub arrays containing each a list of consecutive days? Like this:

Array
(

    [0] => Array
        (
            [2013-07-19] => 1
        )

    [1] => Array
        (
            [2013-07-21] => 3
        )

    [2] => Array
        (
            [2013-07-23] => 2
            [2013-07-24] => 4
            [2013-07-25] => 4
            [2013-07-26] => 2
            [2013-07-27] => 2
        )

    [3] => Array
        (
            [2013-07-30] => 3
            [2013-07-31] => 1
        )

)
$newArray = array();
$i = 0;
foreach ($array as $date => $key) {
    $thisDay = end(explode('-', $date));
    $nextDay = end(explode('-', next($array[$date])));
    if ($thisDay + 1 != $nextDay + 0)
        $i++;
    if (!isset($newArray[$i])) {
        $newArray[$i] = array($date => $key);
    } else {
        if (!isset($newArray[$i][$date])) {
            $newArray[$i][$date] = $key;
        } else {
            $newArray[$i][$date] = $key;
        }//END IF
    }//END IF
}//END FOREACH LOOP

This is the code i would use to do what you are asking.

UPDATE:

I was wrong above. i have altered the code to work this time:

$newArray = array();
$i = 0;
foreach ($array as $date => $key) {
    $thisDay = end(explode('-', $date));
    $nextDay = array_key_exists(date('Y-m-d', strtotime($date . ' +1 day')), $array);
    if (!isset($newArray[$i])) {
        $newArray[$i] = array($date => $key);
    } else {
        if (!isset($newArray[$i][$date])) {
            $newArray[$i][$date] = $key;
        } else {
            $newArray[$i][$date] = $key;
        }//END IF
    }//END IF
    if (!$nextDay)
        $i++;
}//END FOREACH LOOP

Here is my test case:

<?php
$array = array(
    '2013-07-19' => 1,
    '2013-07-21' => 3,
    '2013-07-23' => 2,
    '2013-07-24' => 4,
    '2013-07-25' => 4,
    '2013-07-26' => 2,
    '2013-07-27' => 2,
    '2013-07-30' => 3,
    '2013-07-31' => 1
);
echo '<pre>' . print_r($array, true) . '</pre>';
$newArray = array();
$i = 0;
foreach ($array as $date => $key) {
    $thisDay = end(explode('-', $date));
    $nextDay = array_key_exists(date('Y-m-d', strtotime($date . ' +1 day')), $array);
    if (!isset($newArray[$i])) {
        $newArray[$i] = array($date => $key);
    } else {
        if (!isset($newArray[$i][$date])) {
            $newArray[$i][$date] = $key;
        } else {
            $newArray[$i][$date] = $key;
        }//END IF
    }//END IF
    if (!$nextDay)
        $i++;
}//END FOREACH LOOP
echo '<pre>' . print_r($newArray, true) . '</pre>';
?>
$newArray = array();

foreach ($array as $date => $value)
{
    // Make sure the newArray starts off with at least one element
    if (empty($newArray))
        $newArray[] = array();

    // Calculate the difference in dates.
    // (I like using DateTime, but use whichever method you like)
    $dateTime = new DateTime($date);
    $lastDateTime = new DateTime($lastDate);
    $dateDiff = $dateTime->diff($lastDateTime);

    // Add a new array to the end if the difference between this element and the last was more than a day
    if ($dateDiff->days > 1)
        $newArray[] = array();

    // We can now be guaranteed that the last element of $newArray is the one we want to append to
    $newArray[count($newArray) - 1][$date] = $value;

    // Keep track of the last date you saw
    $lastDate = $date;
}

Here it is in action: https://eval.in/38039

you can do it like this:

$data = array(
  '2013-07-19' => 1,
  '2013-07-21' => 3,
  '2013-07-23' => 2,
  '2013-07-24' => 4,
  '2013-07-25' => 4,
  '2013-07-26' => 2,
  '2013-07-27' => 2,
  '2013-07-30' => 3,
  '2013-07-31' => 1
);

$result = array();
$ref = new DateTime('1821-11-11');
foreach ($data as $datum => $nb) {
    if ($ref->add(new DateInterval('P1D'))->format('Y-m-d')!=$datum) {
        $result[] = array(); 
        $ref = new DateTime($datum);
    }
    $result[array_pop(array_keys($result))][$datum] = $nb;
}
print_r($result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM