1

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 (i.e.: 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.

6
  • Where did the array come from? Can you just create it as you need it? Commented Mar 20, 2017 at 1:11
  • Upvote for having well described your problem and test. Commented Mar 20, 2017 at 1:22
  • Firstly, should this $k = $item['NDC_Item']; not be $k = $item['NDC_Type']; ?? Commented Mar 20, 2017 at 1:25
  • @Rob: you're right. I've correct the code. I voluntarily want to simplify the code but miss some parts. Thanks. Commented Mar 20, 2017 at 1:31
  • @F__M: thanks, I think it's the base of SO. Commented Mar 20, 2017 at 1:31

1 Answer 1

2

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

see this live demo

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure what you mean by #1, but #2 is completely right. was just figuring that out now - added my fiddle to your answer
The values in your $result array are indexed by the NDC_Item value. Whereas if you look at OPs desired output, the array should be indexed by the NDC_Type value.
nice, I didn't see that. fixed fiddle

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.