简体   繁体   English

在PHP中过滤多维数组

[英]Filter a multidimentional array in PHP

I have an array in PHP like this: 我有一个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,
    ),
);

I need to merge some of the entries only if: 我需要合并一些条目only当:

  • The dates are consecutive . 日期是consecutive

  • The items and item rates are the same. itemsitem rates是相同的。


Desired output: 期望的输出:

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

What I have actually is the piece of code to merge the dates when the item and the item-price are the same: 我实际上是在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);

And another one to check if the dates are consecutive or not: 另一个检查日期是否连续:

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

But how can I merge the two codes to get the new array ? 但是如何合并这两个代码来获得新数组呢?

Thanks. 谢谢。

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. 不要使用第二种方法来剖析每个部分,你所做的只是建立你最初的foreach循环中已有的东西。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM