我有这个数组:
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;
我在这里错过了什么?
谢谢。
发布于 2018-07-07 03:34:10
您可以重新构造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
)。
https://stackoverflow.com/questions/51216498
复制相似问题