将数组拆分为连续日期的子数组


Split an array in sub arrays of consecutive dates

对于租金应用程序,我有一个数组$dates,如下所示:

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
)

日期是关键,值是特定产品当天租赁的物品数量

如何将此数组拆分为多个子数组,每个子数组包含连续天数的列表?像这样:

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();
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;
}

它在这里发挥作用:https://eval.in/38039

$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

这是我用来做你要求的事情的代码。

更新:

我错了。这次我修改了代码:

$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

这是我的测试用例:

<?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>';
?>

您可以这样做:

$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);