在PHP编程中,组合统计是一种常见的数据处理方法,它可以帮助我们快速获取数据的各种统计信息。以下是一个使用PHP进行组合统计的实例,我们将通过一个简单的数组来演示如何实现。
实例描述
假设我们有一个包含学生成绩的数组,我们需要统计每个学生的总分、平均分以及最高分。

数据准备
我们准备一个包含学生成绩的数组:
```php
$grades = [
['name' => '张三', 'math' => 85, 'english' => 90, 'science' => 78],
['name' => '李四', 'math' => 92, 'english' => 88, 'science' => 80],
['name' => '王五', 'math' => 75, 'english' => 85, 'science' => 82],
['name' => '赵六', 'math' => 88, 'english' => 90, 'science' => 85]
];
```
实现步骤
1. 遍历数组,计算每个学生的总分、平均分和最高分。
2. 将计算结果存储到新的数组中。
3. 输出结果。
PHP代码实现
```php
$grades = [
['name' => '张三', 'math' => 85, 'english' => 90, 'science' => 78],
['name' => '李四', 'math' => 92, 'english' => 88, 'science' => 80],
['name' => '王五', 'math' => 75, 'english' => 85, 'science' => 82],
['name' => '赵六', 'math' => 88, 'english' => 90, 'science' => 85]
];
// 初始化结果数组
$result = [];
// 遍历学生成绩数组
foreach ($grades as $student) {
// 计算总分
$total = $student['math'] + $student['english'] + $student['science'];
// 计算平均分
$average = $total / 3;
// 计算最高分
$max = max($student['math'], $student['english'], $student['science']);
// 将结果存储到结果数组中
$result[] = [
'name' => $student['name'],
'total' => $total,
'average' => $average,
'max' => $max
];
}
// 输出结果
echo "









