从数组中,如果日期在PHP中是连续的,则为group元素

时间:2017-03-20 01:05:17

标签: php arrays

在PHP中,如果每个元素的日期是连续的,我试图对元素进行分组,如果

  • NDC_Item
  • NDC_Type
  • NDC_Rate
  • NDC_Taxes
  • NDC_TaxesName
  • NDC_Quantity
  • NDC_Status

是平等的。

实际上我可以测试的代码如下:

Array (
    [0] => Array (
        [NDC_Date] => 2017-03-27
        [NDC_Item] => 0
        [NDC_Type] => Night
        [NDC_Rate] => 12.00
        [NDC_Taxes] => 0
        [NDC_TaxesName] => 0
        [NDC_Quantity] => 1
        [NDC_Status] => NotCharged
    )
    [1] => Array (
        [NDC_Date] => 2017-03-28
        [NDC_Item] => 0
        [NDC_Type] => Night
        [NDC_Rate] => 12.00
        [NDC_Taxes] => 0
        [NDC_TaxesName] => 0
        [NDC_Quantity] => 1
        [NDC_Status] => NotCharged
    )
    [2] => Array (
        [NDC_Date] => 2017-03-29
        [NDC_Item] => 0
        [NDC_Type] => Night
        [NDC_Rate] => 12.00
        [NDC_Taxes] => 0
        [NDC_TaxesName] => 0
        [NDC_Quantity] => 1
        [NDC_Status] => NotCharged
    )
    [3] => Array (
        [NDC_Date] => 2017-03-30
        [NDC_Item] => 0
        [NDC_Type] => Night
        [NDC_Rate] => 12.00
        [NDC_Taxes] => 0
        [NDC_TaxesName] => 0
        [NDC_Quantity] => 1
        [NDC_Status] => NotCharged
    )
    [4] => Array (
        [NDC_Date] => 2017-03-31
        [NDC_Item] => 0
        [NDC_Type] => Night
        [NDC_Rate] => 12.00
        [NDC_Taxes] => 0
        [NDC_TaxesName] => 0
        [NDC_Quantity] => 1
        [NDC_Status] => NotCharged
    )
    [5] => Array (
        [NDC_Date] => 2017-04-01
        [NDC_Item] => 0
        [NDC_Type] => Night
        [NDC_Rate] => 12.00
        [NDC_Taxes] => 0
        [NDC_TaxesName] => 0
        [NDC_Quantity] => 1
        [NDC_Status] => NotCharged
    )
    [6] => Array (
        [NDC_Date] => 2017-04-02
        [NDC_Item] => 0
        [NDC_Type] => Night
        [NDC_Rate] => 12.00
        [NDC_Taxes] => 0
        [NDC_TaxesName] => 0
        [NDC_Quantity] => 1
        [NDC_Status] => NotCharged
    )
    [7] => Array (
        [NDC_Date] => 2017-04-03
        [NDC_Item] => 0
        [NDC_Type] => Night
        [NDC_Rate] => 12.00
        [NDC_Taxes] => 0
        [NDC_TaxesName] => 0
        [NDC_Quantity] => 1
        [NDC_Status] => NotCharged
    )
)

它是一个包含客户预订数据的数组。

foreach ($array as $item) {
    $k = $item['NDC_Item'];

    if (!isset($result[$k])) {
        $result[$k] = $item;
    } elseif (
        ($i = $result[$k]) && 
        $item['NDC_Rate'] === $i['NDC_Rate'] && 
        $item['NDC_Type'] === $i['NDC_Type'] && 
        $item['NDC_Taxes'] === $i['NDC_Taxes'] && 
        $item['NDC_TaxesName'] === $i['NDC_TaxesName'] && 
        $item['NDC_Quantity'] === $i['NDC_Quantity'] &&
        $item['NDC_Status']=== $i['NDC_Status'] 
    ) {
        $current_dates = explode(', ', $result[$k]['NDC_Date']);
        $last_date = end($current_dates);
        if(date('Y-m-d', strtotime("{$last_date} +1 day")) === $item['NDC_Date']) {
            $result[$k]['NDC_Id'] .= ','. $item['NDC_Id'];
            $result[$k]['NDC_Date'] .= ','. $item['NDC_Date'];
        } else {
            $result[$k. microtime()] = $item;
        }
    } else {
        $result[$k. microtime()] = $item;
    }
}

问题是:实际上,前面的代码合并前两个元素(即:2017-03-27和2017-03-28分组)但不是2017-03-29,2017-03-30,.. 。,而它连续......

预期结果应为:

Array (
    [Night] => Array (
        [NDC_Date] => 2017-03-27,2017-03-28,2017-03-29,2017-03-30,2017-03-31,2017-04-01,2017-04-02,2017-04-03
        [NDC_Item] => 0
        [NDC_Type] => Night
        [NDC_Rate] => 12.00
        [NDC_Taxes] => 0
        [NDC_TaxesName] => 0
        [NDC_Quantity] => 1
        [NDC_Status] => NotCharged
    )
)

任何想法为什么?

感谢。

1 个答案:

答案 0 :(得分:2)

你的代码犯了两个错误。

  1. $k = $item['NDC_Item'];应为$k = $item['NDC_Type'];

  2. 您正在', '(以逗号跟随空格)爆炸日期。而您只是用逗号附加新日期。 $current_dates = explode(', ', $result[$k]['NDC_Date']);$result[$k]['NDC_Date'] .= ','. $item['NDC_Date'];

  3. see this live demo