将2d数组中的每三行分组,并对一列求和



我需要将3行分组在一起,对它们的工资值求和,并访问下一行(下一个日历月(的月份名称。

$salaries =
[
[
'month'=>'January',
'salary'=>200
],
[
'month'=>'Februray',
'salary'=>300
],
[
'month'=>'March',
'salary'=>400
],
[
'month'=>'April',
'salary'=>500
],
[
'month'=>'May',
'salary'=>600
],
[
'month'=>'June',
'salary'=>700
],
[
'month'=>'July',
'salary'=>800
],
[
'month'=>'August',
'salary'=>900
],
[
'month'=>'September',
'salary'=>1000
],
[
'month'=>'October',
'salary'=>1100
],
[
'month'=>'November',
'salary'=>130
],
[
'month'=>'December',
'salary'=>1200
]
];

我试过这个页面上的代码,但我不知道如何用总和指定第4个月。

如果可能的话,我想要这个:

Array
(
[0] => 
[
'month'=>'April'
'sum'=900
]
[1] => 
[
'month'=>'other month'
'sum'=1800
]
[2] => 
[
'month'=>'other month'
'sum'=2700
]
[3] => 
[
'month'=>'other month'
'sum'=2430
]
)

我试过这个代码:

$groupSum = []; 
foreach (array_chunk($salaries , 3) as $key => $value) { 
$groupSum[] = array_reduce($value, function ($sum, $item) { 
$sum += $item['salary']; return $sum; 
}); 
}

结果是:

Array ( 
[0] => 900
[1] => 1800
[2] => 2700
[3] => 2430
)

一个(非常(基本的解决方案(对于我的测试,我在二月月份名称上修复了拼写错误(:

function getNextMonth($month) {
$months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
$pos = array_search($month, $months);
if ($pos === false) {
throw new Exception('Invalid month');
}
if ($pos == 11) {
return 'January';
// should return also year + 1
}
return $months[$pos+1];
}
$i = 1;
$trimestreSum = 0;
$trimestres = [];
foreach($salaries as $month) {
$trimestreSum += $month['salary'];
if ($i % 3 == 0) {
array_push($trimestres, [
'month' => getNextMonth($month['month']),
'sum' => $trimestreSum,
]);
$i = 0;
$trimestreSum = 0;
}
$i++;
}
print_r($trimestres);

结果是

Array
(
[0] => Array
(
[month] => April
[sum] => 900
)
[1] => Array
(
[month] => July
[sum] => 1800
)
[2] => Array
(
[month] => October
[sum] => 2700
)
[3] => Array
(
[month] => January
[sum] => 2430
)
)

getNextMonth可能是您需要添加到自己代码中的唯一一段代码:(

很确定有更短(更优雅(的解决方案,但希望无论如何都能有所帮助。

试试这个

$groups = [];
$fourth = $salaries[3]['month'];
foreach(array_chunk($salaries,3) as $salary) {
$sum = 0;
$month = 'other month';
foreach($salary as $k => $sal) {
$sum += $sal['salary'];
if($fourth == $sal['month']) {
$month = $sal['month'];
}
}
$groups[] = [
'month' => $month,
'sum' => $sum
];
}

结果

array(4) {
[0]=>
array(2) {
["month"]=>
string(11) "other month"
["sum"]=>
int(900)
}
[1]=>
array(2) {
["month"]=>
string(5) "April"
["sum"]=>
int(1800)
}
[2]=>
array(2) {
["month"]=>
string(11) "other month"
["sum"]=>
int(2700)
}
[3]=>
array(2) {
["month"]=>
string(11) "other month"
["sum"]=>
int(2430)
}
}

更新:@Armage的答案似乎奏效了,但只是为了好玩:

$result = [];
// I guess $iMax can just be 12, but I don't know
for($nextTriI = 3, $iMax = count($salaries); $nextTriI < $iMax; $nextTriI += 3) {
$triSum = 0;
for($m = $nextTriI - 3; $m < $nextTriI; $m++) {
$triSum += $salaries[$m]['salary'];
}
$result[] = [
'month' => $salaris[$nextTriI]['month'], 
'sum' => $triSum
];
}
print_r($result);

当然,这假设了有效的数据、基于索引的$salaries


后人的老答案:

我不确定这是你想要的,这不是最好的方法,但也许:

$groupSum = []; 
foreach (array_chunk($salaries , 4) as $value) { 
$groupSum[] = array_reduce(
$value, 
function ($total, $item) { 
$total['sum'] += $item['salary']; 
$total['month'] = $item['month'];
return $total; 
},
['sum' => 0]
); 
}

通过实现;参考";(参见摘录中的&$ref(,并将其推送到结果数组中以表示";组";,您可以简单地将随后遇到的数据推送到该引用中,而不是跟踪结果数组中该组的实际键/位置。

要确定新组何时开始,请使用模数计算(换句话说,在执行除法后获得余数(并检查该值是否为零。

要获得第三个月之后的月份名称,请在当前索引上加3,然后加上模数值(基于12个月的周期(,以便最后一个季度获得第一个月的名称。

代码:(演示(

$result = [];
foreach ($salaries as $i => $row) {
if (!($i % 3)) {
unset($ref);
$result[] = &$ref;
$ref['month'] = $salaries[($i + 3) % 12]['month'];
}
$ref['salary'] = ($ref['salary'] ?? 0) + $row['salary'];
}
var_export($result);

输出:

array (
0 => 
array (
'month' => 'April',
'salary' => 900,
),
1 => 
array (
'month' => 'July',
'salary' => 1800,
),
2 => 
array (
'month' => 'October',
'salary' => 2700,
),
3 => 
array (
'month' => 'January',
'salary' => 2430,
),
)

@韦尔森说:

是否可以列出其他月份,而不修改季度的计算,以便我们可以显示所有月份?

是的,像这样:

代码:(演示(

$result = [];
foreach ($salaries as $row) {
if (!isset($ref) || count($ref['months']) >= 3) {
unset($ref);
$result[] = &$ref;
}
$ref['months'][] = $row['month'];
$ref['salary'] = ($ref['salary'] ?? 0) + $row['salary'];
}
var_export($result);

输出:

array (
0 => 
array (
'months' => 
array (
0 => 'January',
1 => 'Februray',
2 => 'March',
),
'salary' => 900,
),
1 => 
array (
'months' => 
array (
0 => 'April',
1 => 'May',
2 => 'June',
),
'salary' => 1800,
),
2 => 
array (
'months' => 
array (
0 => 'July',
1 => 'August',
2 => 'September',
),
'salary' => 2700,
),
3 => 
array (
'months' => 
array (
0 => 'October',
1 => 'November',
2 => 'December',
),
'salary' => 2430,
),
)

相关内容

  • 没有找到相关文章

最新更新