2 回答
TA贡献1824条经验 获得超5个赞
您可以只映射主数组并推送其元素的总和:
$array['main'] = array_map(static function($counts) {
$sum = ctype_digit(implode('', $counts)) ? array_sum($counts) : '-';
return array_merge($counts, [$sum]);
}, $array['main']);
密钥的值main将是您实际需要的:
array(2) {
["firstYearStudents"]=>
array(3) {
[0] => int(10)
[1] => int(12)
[2] => int(22)
}
["secondYearStudents"]=>
array(3) {
[0] => int(8)
[1] => int(9)
[2] => int(17)
}
["programCode"]=>
array(3) {
[0] => string(8) "03.02.01"
[1] => string(8) "03.01.01"
[2] => string(1) "-"
}
}
TA贡献1847条经验 获得超11个赞
如果您只需要移动这两个属性,这应该是一个简单的解决方案:
$data = [
'main' => [
'firstYearStudents' => [
'10',
'12'
],
'secondYearStudents' => [
'8',
'9'
]
],
'total' => [
'totalFirstYear' => '22',
'totalSecondYear' => '17'
]
];
$data['main']['firstYearStudents'][] = $data['total']['totalFirstYear'];
$data['main']['secondYearStudents'][] = $data['total']['totalSecondYear'];
如有必要,您应该能够扩展它以涵盖更多年,或者最终编写一个循环来管理您需要的年数。
- 2 回答
- 0 关注
- 148 浏览
添加回答
举报