简体   繁体   中英

From an array, group element if dates are consecutive in PHP

In PHP, I'm trying to group elements if the date of each element are consecutive and if

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

are equal.


Actually the code I can test is the following:

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

It's an array containing datas of a customer booking.


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

The problem is: actually, the previous code merge the first two elements (ie: 2017-03-27 and 2017-03-28 are grouped) but not 2017-03-29, 2017-03-30, ..., whereas it's consecutive...


The expected results should be:

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

Any ideas why ?

Thanks.

You've made two mistakes in your code.

  1. $k = $item['NDC_Item']; should be $k = $item['NDC_Type'];

  2. You're exploding the dates on ', ' (comma with following space). Whereas you're appending new dates with only a comma. $current_dates = explode(', ', $result[$k]['NDC_Date']); and $result[$k]['NDC_Date'] .= ','. $item['NDC_Date']; $result[$k]['NDC_Date'] .= ','. $item['NDC_Date'];

see this live demo

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