在PHP中过滤多维数组

时间:2017-01-10 00:20:14

标签: php arrays

我在PHP中有一个像这样的数组:

Array(
    0 => Array(
        'NDC_Date'     => '2017-02-26',
        'NDC_Item'     => 'Night',
        'NDC_Rate'     => 108.00,
        'NDC_Quantity' => 1,
    ),
    1 => Array(
        'NDC_Date'     => '2017-02-27',
        'NDC_Item'     => 'Night',
        'NDC_Rate'     => 108.00,
        'NDC_Quantity' => 1,
    ),
    2 => Array(
        'NDC_Date'     => '2017-02-28',
        'NDC_Item'     => 'Night',
        'NDC_Rate'     => 118.00,
        'NDC_Quantity' => 1,
    ),
    3 => Array(
        'NDC_Date'     => '2017-03-01',
        'NDC_Item'     => 'Night',
        'NDC_Rate'     => 108.00,
        'NDC_Quantity' => 1,
    ),
    4 => Array(
        'NDC_Date'     => '2017-01-01',
        'NDC_Item'     => 'Breakfast',
        'NDC_Rate'     => 12.00,
        'NDC_Quantity' => 2,
    ),
    5 => Array(
        'NDC_Date'     => '2017-01-01',
        'NDC_Item'     => 'Extra bed',
        'NDC_Rate'     => 0.00,
        'NDC_Quantity' => 1,
    ),
    6 => Array(
        'NDC_Date'     => '2017-01-02',
        'NDC_Item'     => 'Breakfast',
        'NDC_Rate'     => 12.00,
        'NDC_Quantity' => 2,
    ),
    7 => Array(
        'NDC_Date'     => '2017-01-02',
        'NDC_Item'     => 'Extra bed',
        'NDC_Rate'     => 0.00,
        'NDC_Quantity' => 1,
    ),
    8 => Array(
        'NDC_Date'     => '2017-01-03',
        'NDC_Item'     => 'Breakfast',
        'NDC_Rate'     => 12.00,
        'NDC_Quantity' => 2,
    ),
    9 => Array(
        'NDC_Date'     => '2017-01-03',
        'NDC_Item'     => 'Extra bed',
        'NDC_Rate'     => 0.00,
        'NDC_Quantity' => 1,
    ),
);

我需要合并一些条目only if:

  • 日期为consecutive

  • itemsitem rates相同。

期望的输出:

Array (
    [0] => Array (
        [NDC_Date] => 2017-02-26, 2017-02-27
        [NDC_Item] => Night
        [NDC_Rate] => 108.00
        [NDC_Quantity] => 1
    )
    [1] => Array (
        [NDC_Date] => 2017-02-28
        [NDC_Item] => Night
        [NDC_Rate] => 118.00
        [NDC_Quantity] => 1
    )
    [2] => Array (
        [NDC_Date] => 2017-03-01
        [NDC_Item] => Night
        [NDC_Rate] => 108.00
        [NDC_Quantity] => 1
    )
    [3] => Array (
        [NDC_Date] => 2017-01-01, 2017-01-02, 2017-01-03
        [NDC_Item] => Breakfast
        [NDC_Rate] => 12.00
        [NDC_Quantity] => 2
    )
    [4] => Array (
        [NDC_Date] => 2017-01-01, 2017-01-02, 2017-01-03
        [NDC_Item] => Extra bed
        [NDC_Rate] => 0.00
        [NDC_Quantity] => 1
    )
)

我实际上是在itemitem-price相同时合并日期的代码片段:

$result = [];
foreach ($billingDatas 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_Quantity'] == $i['NDC_Quantity']) {
        $result[$k]['NDC_Date'] .= ', '. $item['NDC_Date'];
    } else {
        $result[$k. time()] = $item;
    }
}
$result = array_values($result);

另一个检查日期是否连续的人:

$dates = "2017-02-26, 2017-02-27, 2017-03-01";
$dates = explode(',', $dates);

$conseq = array(); 
$ii = 0;
$max = count($dates);

for($i = 0; $i < count($dates); $i++) {
    $conseq[$ii][] = date('Y-m-d',strtotime($dates[$i]));

    if($i + 1 < $max) {
        $dif = strtotime($dates[$i + 1]) - strtotime($dates[$i]);
        if($dif >= 90000) {
            $ii++;
        }   
    }
}

print_r($conseq);

但是如何合并这两个代码来获得新数组呢?

感谢。

1 个答案:

答案 0 :(得分:0)

Don't use the second approach of dissecting each part, what you do is just to build on what you already have in your initial foreach loop.

First criteria in your current loop is to check the rate and then quantity. The next step is to just check the date.

To do that, get the current batch grouped array, explode it and get the last date. And check the current iteration date and check if that date is the consecutive day.

Idea:

$result = [];
foreach ($billingDatas 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_Quantity'] === $i['NDC_Quantity'] 
    ) {
        // additional date check
        $current_dates = explode(', ', $result[$k]['NDC_Date']); // get the comma delimited dates and explode
        $last_date = end($current_dates); // get the last date
        // then check if the current iteration date is the next day
        if(date('Y-m-d', strtotime("{$last_date} +1 day")) === $item['NDC_Date']) {
            $result[$k]['NDC_Date'] .= ', '. $item['NDC_Date'];
        } else {
            $result[$k. microtime()] = $item;
        }
        // rest is self-explanatory

    } else {
        $result[$k. microtime()] = $item;
    }
}
$result = array_values($result);