使用PHP过滤数组

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (96)

我有这个数组:

Array
(
    [0] => Array
        (
            [DEB_Date] => 2018-01-06
            [DEB_Total] => 100.00
            [DEB_Nb_Days] => 181
        )
    [1] => Array
        (
            [DEB_Date] => 2018-07-06
            [DEB_Total] => 100.00
            [DEB_Nb_Days] => 0
        )
)

我希望有一种报告取决于DEB_Nb_Days

所以我的脚本如下:

// Init the array
$arr['0_to_30']['DEB_Total'] = 0;
$arr['31_to_60']['DEB_Total'] = 0;
$arr['61_to_90']['DEB_Total'] = 0;
$arr['91_to_120']['DEB_Total'] = 0;
$arr['121_and_more']['DEB_Total'] = 0;

// Loop between the debtors
foreach($debtors as $debtor){
    if($debtor['DEB_Nb_Days'] >= 0  || $debtor['DEB_Nb_Days'] <= 30) { $arr['0_to_30']['DEB_Total'] += $debtor['DEB_Total']; }
    if($debtor['DEB_Nb_Days'] >= 31 || $debtor['PAY_Nb_Days'] <= 60) { $arr['31_to_60']['DEB_Total'] += $debtor['DEB_Total']; }
    if($debtor['DEB_Nb_Days'] >= 61 || $debtor['PAY_Nb_Days'] <= 90) { $arr['61_to_90']['DEB_Total'] += $debtor['DEB_Total']; }
    if($debtor['DEB_Nb_Days'] >= 91 || $debtor['PAY_Nb_Days'] <= 120) { $arr['91_to_120']['DEB_Total'] += $debtor['DEB_Total']; }
    if($debtor['PAY_Nb_Days'] >= 121) { $arr['121_and_more']['DEB_Total'] += $debtor['DEB_Total']; }
}

我应该这样:

$arr['0_to_30']['DEB_Total'] = 100;
$arr['31_to_60']['DEB_Total'] = 0;
$arr['61_to_90']['DEB_Total'] = 0;
$arr['91_to_120']['DEB_Total'] = 0;
$arr['121_and_more']['DEB_Total'] = 100;

但实际上结果是:

$arr['0_to_30']['DEB_Total'] = 200;
$arr['31_to_60']['DEB_Total'] = 200;
$arr['61_to_90']['DEB_Total'] = 200;
$arr['91_to_120']['DEB_Total'] = 200;
$arr['121_and_more']['DEB_Total'] = 0;

我在这里错过了什么?

谢谢。

提问于
用户回答回答于

您可以重构if条件集以查找最高的条件然后用于else减少比较次数......

foreach ($debtors as $debtor) {
    if ($debtor['DEB_Nb_Days'] >= 121) {
        $arr['121_and_more']['DEB_Total'] += $debtor['DEB_Total'];
    } 
    else if ($debtor['DEB_Nb_Days'] >= 91) {
        $arr['91_to_120']['DEB_Total'] += $debtor['DEB_Total'];
    } 
    else if ($debtor['DEB_Nb_Days'] >= 61) {
        $arr['61_to_90']['DEB_Total'] += $debtor['DEB_Total'];
    } 
    else if ($debtor['DEB_Nb_Days'] >= 31) {
        $arr['31_to_60']['DEB_Total'] += $debtor['DEB_Total'];
    } 
    else {
        $arr['0_to_30']['DEB_Total'] += $debtor['DEB_Total'];
    }
}

(这也纠正了你在某些时候使用PAY_Nb_Days而不是DEB_Nb_Days)。

用户回答回答于
// Loop between the debtors
foreach($debtors as $debtor){
    if($debtor['DEB_Nb_Days'] >= 0  && $debtor['DEB_Nb_Days'] <= 30) { $arr['0_to_30']['DEB_Total'] += $debtor['DEB_Total']; }
    if($debtor['DEB_Nb_Days'] >= 31 && $debtor['PAY_Nb_Days'] <= 60) { $arr['31_to_60']['DEB_Total'] += $debtor['DEB_Total']; }
    if($debtor['DEB_Nb_Days'] >= 61 && $debtor['PAY_Nb_Days'] <= 90) { $arr['61_to_90']['DEB_Total'] += $debtor['DEB_Total']; }
    if($debtor['DEB_Nb_Days'] >= 91 && $debtor['PAY_Nb_Days'] <= 120) { $arr['91_to_120']['DEB_Total'] += $debtor['DEB_Total']; }
    if($debtor['PAY_Nb_Days'] >= 121) { $arr['121_and_more']['DEB_Total'] += $debtor['DEB_Total']; }
}

扫码关注云+社区

领取腾讯云代金券