使用PHP根据范围合并一些数组数据
我有一个数组:使用PHP根据范围合并一些数组数据,php,arrays,Php,Arrays,我有一个数组: Array ( [0] => Array ( [Sum] => 125.00 [PastDays] => 530 ) [1] => Array ( [Sum] => 123.00 [PastDays] => 110 ) [2] => Array
Array
(
[0] => Array
(
[Sum] => 125.00
[PastDays] => 530
)
[1] => Array
(
[Sum] => 123.00
[PastDays] => 110
)
[2] => Array
(
[Sum] => 500.00
[PastDays] => 5
)
[3] => Array
(
[Sum] => 500.00
[PastDays] => 1
)
)
我将根据支付天数合并这些数据,以在数组中具有此类型:
- 如果
PAY\u Nb\u Days
介于0
和30
之间
- 如果
PAY\u Nb\u Days
介于31
和60
之间
- 如果
PAY\u Nb\u Days
介于61
和90
之间
- 如果
PAY\u Nb\u Days
介于91
和120
之间
- 如果
PAY\u Nb\u Days
高于121
比如说:
Array
(
['Between 0 and 30'] => Array
(
[Sum] => 1000.00
)
['Between 31 and 60'] => Array
(
[Sum] => 0.00
)
['Between 61 and 90'] => Array
(
[Sum] => 0.00
)
['Between 91 and 120'] => Array
(
[Sum] => 123.00
)
['Hight than 121'] => Array
(
[Sum] => 125.00
)
)
你能告诉我从哪里开始吗
以下是我所尝试的:
foreach($items as $item) {
if($item[PastDays] <= 30) { $sum_0_to_30 += $item['Sum']; }
if($item[PastDays] >= 31 && $item[PastDays] <= 60) { $sum_31_to_60 += $item['Sum']; }
if($item[PastDays] >= 61 && $item[PastDays] <= 90) { $sum_61_to_90 += $item['Sum']; }
if($item[PastDays] >= 91 && $item[PastDays] <= 120) { $sum_91_to_120 += $item['Sum']; }
if($item[PastDays] >= 121) { $sum_121 += $item['Sum']; }
}
foreach($items作为$item){
如果($item[PastDays]=31&&$item[PastDays]=61&&$item[PastDays]=91&&$item[PastDays]=121){$sum_121+=$item['sum'];}
}
谢谢。$arr=[
$arr = [
'Between 0 and 30' => ['Sum' => 0],
'Between 31 and 60' => ['Sum' => 0],
'Between 61 and 90' => ['Sum' => 0],
'Between 91 and 120' => ['Sum' => 0],
'Hight than 121' => ['Sum' => 0],
];
foreach($items as $item){
switch($item['PastDays']){
case ($item['PastDays'] < 31):
$arr['Between 0 and 30']['Sum'] += $item['Sum'];
break;
case ($item['PastDays'] < 61):
$arr['Between 31 and 60']['Sum'] += $item['Sum'];
break;
case ($item['PastDays'] < 91):
$arr['Between 61 and 90']['Sum'] += $item['Sum'];
break;
case ($item['PastDays'] < 121):
$arr['Between 91 and 120']['Sum'] += $item['Sum'];
break;
default:
$arr['Hight than 121']['Sum'] += $item['Sum'];
break;
}
}
'介于0和30之间'=>,
'介于31和60之间'=>,
'介于61和90'=>,
'介于91和120之间'=>,
'高于121'=>0],
];
foreach($items作为$item){
开关($item['passdays'])){
案例($item['passdays']<31):
$arr['介于0和30']['Sum']+=$item['Sum'];
打破
案例($item['passdays']<61):
$arr['介于31和60']['Sum']+=$item['Sum'];
打破
案例($item['passdays']<91):
$arr['介于61和90'之间]['Sum']+=$item['Sum'];
打破
案例($item['passdays']<121):
$arr['介于91和120'之间]['Sum']+=$item['Sum'];
打破
违约:
$arr['Hight than 121']['Sum']+=$item['Sum'];
打破
}
}
$arr
将包含您需要的信息您所拥有的有什么问题?完成后,您可以将所有变量放入一个数组。您可以使用elseif
,然后不需要测试=
。您好,请让我检查一下。