在连续日期的子数组中拆分数组

时间:2013-07-18 16:52:03

标签: php arrays date split

对于租金申请,我有一个这样的数组$日期:

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
        )

)

3 个答案:

答案 0 :(得分: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

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

更新:

上面我错了。我这段时间已经改变了代码:

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

答案 1 :(得分: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

答案 2 :(得分:0)

你可以这样做:

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