php项目统计射门次数哪队更多?

wen PHP项目 2

PHP 项目:统计哪队射门次数更多

下面是一个完整的 PHP 示例,帮你统计两支球队的射门次数并判断哪队更多。

php项目统计射门次数哪队更多?

基础版(硬编码数据)

<?php
// 比赛射门统计
$teams = [
    '主队' => ['name' => '曼联', 'shots' => 15],
    '客队' => ['name' => '利物浦', 'shots' => 12],
];
$home = $teams['主队'];
$away = $teams['客队'];
echo "{$home['name']} 射门次数:{$home['shots']}\n";
echo "{$away['name']} 射门次数:{$away['shots']}\n";
if ($home['shots'] > $away['shots']) {
    echo "射门更多的一方:{$home['name']}(多 " . ($home['shots'] - $away['shots']) . " 次)\n";
} elseif ($away['shots'] > $home['shots']) {
    echo "射门更多的一方:{$away['name']}(多 " . ($away['shots'] - $home['shots']) . " 次)\n";
} else {
    echo "双方射门次数相同\n";
}

输出:

曼联 射门次数:15
利物浦 射门次数:12
射门更多的一方:曼联(多 3 次)

从数据库读取(推荐实际项目使用)

建表 SQL

CREATE TABLE match_shots (
    id INT AUTO_INCREMENT PRIMARY KEY,
    match_id INT NOT NULL,
    team_name VARCHAR(50) NOT NULL,
    side ENUM('home','away') NOT NULL,
    shots INT NOT NULL DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

PHP 查询代码

<?php
$pdo = new PDO('mysql:host=localhost;dbname=football;charset=utf8mb4', 'root', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$matchId = 1;
$stmt = $pdo->prepare("
    SELECT side, team_name, shots
    FROM match_shots
    WHERE match_id = :mid
");
$stmt->execute([':mid' => $matchId]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$data = [];
foreach ($rows as $row) {
    $data[$row['side']] = $row;
}
if (!isset($data['home'], $data['away'])) {
    exit("数据不完整,无法比较\n");
}
$home = $data['home'];
$away = $data['away'];
printf("%s 射门 %d 次 | %s 射门 %d 次\n",
    $home['team_name'], $home['shots'],
    $away['team_name'], $away['shots']
);
if ($home['shots'] > $away['shots']) {
    echo "➡️ 射门更多:{$home['team_name']}\n";
} elseif ($away['shots'] > $home['shots']) {
    echo "➡️ 射门更多:{$away['team_name']}\n";
} else {
    echo "➡️ 两队射门次数持平\n";
}

从数组/API 数据聚合

如果你有多名球员的射门数据,需要按球队汇总:

<?php
// 假设是从 API 或表单收集的球员数据
$players = [
    ['team' => 'A队', 'player' => '张三', 'shots' => 5],
    ['team' => 'A队', 'player' => '李四', 'shots' => 4],
    ['team' => 'A队', 'player' => '王五', 'shots' => 6],
    ['team' => 'B队', 'player' => '赵六', 'shots' => 3],
    ['team' => 'B队', 'player' => '钱七', 'shots' => 8],
];
// 按球队汇总
$totals = [];
foreach ($players as $p) {
    $totals[$p['team']] = ($totals[$p['team']] ?? 0) + $p['shots'];
}
// 找出射门最多的球队
arsort($totals); // 降序排序
$topTeam = array_key_first($totals);
echo "各队射门统计:\n";
foreach ($totals as $team => $shots) {
    echo "  {$team}: {$shots} 次\n";
}
echo "射门最多的球队:{$topTeam}({$totals[$topTeam]} 次)\n";

封装成函数(可复用)

<?php
/**
 * 比较两队射门次数
 * @param string $teamA  A队名称
 * @param int    $shotsA A队射门数
 * @param string $teamB  B队名称
 * @param int    $shotsB B队射门数
 * @return array
 */
function compareShots(string $teamA, int $shotsA, string $teamB, int $shotsB): array
{
    if ($shotsA > $shotsB) {
        $winner = $teamA;
        $diff = $shotsA - $shotsB;
    } elseif ($shotsB > $shotsA) {
        $winner = $teamB;
        $diff = $shotsB - $shotsA;
    } else {
        $winner = null;
        $diff = 0;
    }
    return [
        'teamA'  => ['name' => $teamA, 'shots' => $shotsA],
        'teamB'  => ['name' => $teamB, 'shots' => $shotsB],
        'winner' => $winner,
        'diff'   => $diff,
        'is_draw'=> $winner === null,
    ];
}
$result = compareShots('曼城', 18, '切尔西', 9);
var_dump($result);

注意点 说明
数据类型 射门次数用 int,避免字符串比较出错
平局处理 必须单独处理,否则会出现“两边都赢”
SQL 注入 用 PDO 预处理语句
聚合逻辑 多球员数据先按球队 汇总
空数据 检查 isset,防止下标不存在

需要我帮你适配具体的场景吗?

  • 从某个 API 抓取数据(如 Football-Data.org)
  • 用 Laravel / ThinkPHP 实现
  • 加入图表展示(ECharts / Chart.js)
  • 存成 CSV 或 Excel

告诉我你的具体需求,我可以进一步定制。

抱歉,评论功能暂时关闭!

友情链接攻略窝 · 礼包窝 · 配置窝 · 换算窝 · 算法窝 · 查文窝 · 手册窝 · 问答窝 · 全文搜 · 手册库
相关站点礼包窝 · 查文窝