Appearance
7.5 实操:学生列表、商品列表数组处理
本实操将使用PHP数组处理学生列表和商品列表数据。
功能需求
- 学生列表管理:添加、删除、修改学生信息
- 商品列表管理:排序、筛选、统计商品信息
实现代码
1. 学生列表管理
php
<?php
// 初始化学生数组
$students = [
["id" => 1, "name" => "张三", "age" => 18, "score" => 95],
["id" => 2, "name" => "李四", "age" => 19, "score" => 88],
["id" => 3, "name" => "王五", "age" => 17, "score" => 92],
["id" => 4, "name" => "赵六", "age" => 18, "score" => 85],
["id" => 5, "name" => "钱七", "age" => 19, "score" => 90]
];
// 函数:显示学生列表
function showStudents($students) {
echo "<table border='1' cellpadding='10'>";
echo "<tr><th>ID</th><th>姓名</th><th>年龄</th><th>分数</th></tr>";
foreach ($students as $student) {
echo "<tr>";
echo "<td>{$student['id']}</td>";
echo "<td>{$student['name']}</td>";
echo "<td>{$student['age']}</td>";
echo "<td>{$student['score']}</td>";
echo "</tr>";
}
echo "</table>";
}
// 显示初始学生列表
echo "初始学生列表:<br>";
showStudents($students);
// 添加新学生
$newStudent = ["id" => 6, "name" => "孙八", "age" => 17, "score" => 89];
$students[] = $newStudent;
echo "<br>添加新学生后:<br>";
showStudents($students);
// 删除学生(根据ID)
$idToDelete = 3;
$students = array_filter($students, function($student) use ($idToDelete) {
return $student['id'] != $idToDelete;
});
echo "<br>删除ID为3的学生后:<br>";
showStudents($students);
// 修改学生信息
$idToUpdate = 2;
foreach ($students as &$student) {
if ($student['id'] == $idToUpdate) {
$student['score'] = 95; // 修改分数
break;
}
}
echo "<br>修改ID为2的学生分数后:<br>";
showStudents($students);
// 按分数排序
usort($students, function($a, $b) {
return $b['score'] - $a['score']; // 降序
});
echo "<br>按分数降序排序后:<br>";
showStudents($students);
// 计算平均分数
$totalScore = 0;
foreach ($students as $student) {
$totalScore += $student['score'];
}
$averageScore = $totalScore / count($students);
echo "<br>平均分数: " . number_format($averageScore, 2) . "<br>";
?>2. 商品列表管理
php
<?php
// 初始化商品数组
$products = [
["id" => 1, "name" => "iPhone 13", "category" => "手机", "price" => 5999, "stock" => 100],
["id" => 2, "name" => "MacBook Pro", "category" => "电脑", "price" => 9999, "stock" => 50],
["id" => 3, "name" => "AirPods Pro", "category" => "配件", "price" => 1999, "stock" => 200],
["id" => 4, "name" => "iPad Pro", "category" => "平板", "price" => 6999, "stock" => 80],
["id" => 5, "name" => "Apple Watch", "category" => "配件", "price" => 2999, "stock" => 150]
];
// 函数:显示商品列表
function showProducts($products) {
echo "<table border='1' cellpadding='10'>";
echo "<tr><th>ID</th><th>名称</th><th>分类</th><th>价格</th><th>库存</th></tr>";
foreach ($products as $product) {
echo "<tr>";
echo "<td>{$product['id']}</td>";
echo "<td>{$product['name']}</td>";
echo "<td>{$product['category']}</td>";
echo "<td>¥{$product['price']}</td>";
echo "<td>{$product['stock']}</td>";
echo "</tr>";
}
echo "</table>";
}
// 显示初始商品列表
echo "初始商品列表:<br>";
showProducts($products);
// 按价格排序
usort($products, function($a, $b) {
return $a['price'] - $b['price']; // 升序
});
echo "<br>按价格升序排序后:<br>";
showProducts($products);
// 按分类筛选
$category = "配件";
$filteredProducts = array_filter($products, function($product) use ($category) {
return $product['category'] == $category;
});
echo "<br>筛选分类为'配件'的商品:<br>";
showProducts($filteredProducts);
// 按价格范围筛选
$minPrice = 2000;
$maxPrice = 7000;
$priceFiltered = array_filter($products, function($product) use ($minPrice, $maxPrice) {
return $product['price'] >= $minPrice && $product['price'] <= $maxPrice;
});
echo "<br>价格在¥2000-¥7000之间的商品:<br>";
showProducts($priceFiltered);
// 计算总库存和总价值
$totalStock = 0;
$totalValue = 0;
foreach ($products as $product) {
$totalStock += $product['stock'];
$totalValue += $product['price'] * $product['stock'];
}
echo "<br>总库存: $totalStock<br>";
echo "总价值: ¥" . number_format($totalValue) . "<br>";
// 按分类分组统计
$categoryStats = [];
foreach ($products as $product) {
$cat = $product['category'];
if (!isset($categoryStats[$cat])) {
$categoryStats[$cat] = ['count' => 0, 'totalStock' => 0, 'totalValue' => 0];
}
$categoryStats[$cat]['count']++;
$categoryStats[$cat]['totalStock'] += $product['stock'];
$categoryStats[$cat]['totalValue'] += $product['price'] * $product['stock'];
}
echo "<br>按分类统计:<br>";
echo "<table border='1' cellpadding='10'>";
echo "<tr><th>分类</th><th>商品数量</th><th>总库存</th><th>总价值</th></tr>";
foreach ($categoryStats as $cat => $stats) {
echo "<tr>";
echo "<td>$cat</td>";
echo "<td>{$stats['count']}</td>";
echo "<td>{$stats['totalStock']}</td>";
echo "<td>¥" . number_format($stats['totalValue']) . "</td>";
echo "</tr>";
}
echo "</table>";
?>代码解析
学生列表管理:
- 初始化学生数组,包含ID、姓名、年龄和分数
- 实现显示学生列表的函数
- 添加新学生
- 根据ID删除学生
- 修改学生信息
- 按分数排序
- 计算平均分数
商品列表管理:
- 初始化商品数组,包含ID、名称、分类、价格和库存
- 实现显示商品列表的函数
- 按价格排序
- 按分类筛选商品
- 按价格范围筛选商品
- 计算总库存和总价值
- 按分类分组统计
运行结果
执行上述代码后,会在浏览器中显示各种学生和商品管理操作的结果。
扩展练习
- 实现学生信息的搜索功能
- 实现商品的分页显示
- 为商品列表添加排序功能(按不同字段)
- 实现学生和商品的导出功能
