1

对于租金申请,我有一个这样的数组 $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
        )

)
4

3 回答 3

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>';
?>
于 2013-07-18T17:00:18.183 回答
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

于 2013-07-18T17:49:09.477 回答
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);
于 2013-07-18T19:51:08.633 回答