You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP足球赛事文本数据统计:完善缺失功能需求

足球赛事数据统计PHP代码完善方案

功能补充说明

需在原有代码基础上实现两个核心功能:

  1. 统计各队伍主场、客场得分等于13的次数
  2. 当最高主场得分、最高客场得分、最大分差、单场总最高得分存在重复极值时,显示最多5条结果而非仅1条

完善后的完整代码

<?php
// 读取赛事数据文件
$file = fopen('football_data.txt', 'r');
if (!$file) {
    die('无法打开数据文件');
}

// 初始化统计变量
$maxHomeScore = [];
$maxAwayScore = [];
$maxGoalDiff = [];
$maxTotalScore = [];
$score13Count = ['home' => [], 'away' => []];

// 遍历每一行数据
while (($line = fgets($file)) !== false) {
    $line = trim($line);
    if (empty($line)) continue;
    
    // 拆分数据(假设格式:主队,主场得分,客队,客场得分)
    list($homeTeam, $homeScore, $awayTeam, $awayScore) = explode(',', $line);
    $homeScore = (int)$homeScore;
    $awayScore = (int)$awayScore;
    $goalDiff = abs($homeScore - $awayScore);
    $totalScore = $homeScore + $awayScore;

    // 统计得分13的次数
    if ($homeScore === 13) {
        $score13Count['home'][$homeTeam] = isset($score13Count['home'][$homeTeam]) ? $score13Count['home'][$homeTeam] + 1 : 1;
    }
    if ($awayScore === 13) {
        $score13Count['away'][$awayTeam] = isset($score13Count['away'][$awayTeam]) ? $score13Count['away'][$awayTeam] + 1 : 1;
    }

    // 处理最高主场得分(保留所有极值记录)
    if (empty($maxHomeScore) || $homeScore > $maxHomeScore[0]['score']) {
        $maxHomeScore = [['team' => $homeTeam, 'score' => $homeScore, 'opponent' => $awayTeam, 'away_score' => $awayScore]];
    } elseif ($homeScore === $maxHomeScore[0]['score']) {
        $maxHomeScore[] = ['team' => $homeTeam, 'score' => $homeScore, 'opponent' => $awayTeam, 'away_score' => $awayScore];
    }

    // 处理最高客场得分(保留所有极值记录)
    if (empty($maxAwayScore) || $awayScore > $maxAwayScore[0]['score']) {
        $maxAwayScore = [['team' => $awayTeam, 'score' => $awayScore, 'opponent' => $homeTeam, 'home_score' => $homeScore]];
    } elseif ($awayScore === $maxAwayScore[0]['score']) {
        $maxAwayScore[] = ['team' => $awayTeam, 'score' => $awayScore, 'opponent' => $homeTeam, 'home_score' => $homeScore];
    }

    // 处理最大分差(保留所有极值记录)
    if (empty($maxGoalDiff) || $goalDiff > $maxGoalDiff[0]['diff']) {
        $maxGoalDiff = [['home_team' => $homeTeam, 'home_score' => $homeScore, 'away_team' => $awayTeam, 'away_score' => $awayScore, 'diff' => $goalDiff]];
    } elseif ($goalDiff === $maxGoalDiff[0]['diff']) {
        $maxGoalDiff[] = ['home_team' => $homeTeam, 'home_score' => $homeScore, 'away_team' => $awayTeam, 'away_score' => $awayScore, 'diff' => $goalDiff];
    }

    // 处理单场总最高得分(保留所有极值记录)
    if (empty($maxTotalScore) || $totalScore > $maxTotalScore[0]['total']) {
        $maxTotalScore = [['home_team' => $homeTeam, 'home_score' => $homeScore, 'away_team' => $awayTeam, 'away_score' => $awayScore, 'total' => $totalScore]];
    } elseif ($totalScore === $maxTotalScore[0]['total']) {
        $maxTotalScore[] = ['home_team' => $homeTeam, 'home_score' => $homeScore, 'away_team' => $awayTeam, 'away_score' => $awayScore, 'total' => $totalScore];
    }
}

fclose($file);

// 输出统计结果
echo "<h3>最高主场得分</h3>";
foreach (array_slice($maxHomeScore, 0, 5) as $item) {
    echo "- {$item['team']} {$item['score']} vs {$item['opponent']} {$item['away_score']}<br>";
}

echo "<h3>最高客场得分</h3>";
foreach (array_slice($maxAwayScore, 0, 5) as $item) {
    echo "- {$item['team']} {$item['score']} @ {$item['opponent']} {$item['home_score']}<br>";
}

echo "<h3>最大分差</h3>";
foreach (array_slice($maxGoalDiff, 0, 5) as $item) {
    echo "- {$item['home_team']} {$item['home_score']} vs {$item['away_team']} {$item['away_score']} (分差: {$item['diff']})<br>";
}

echo "<h3>单场总最高得分</h3>";
foreach (array_slice($maxTotalScore, 0, 5) as $item) {
    echo "- {$item['home_team']} {$item['home_score']} vs {$item['away_team']} {$item['away_score']} (总分: {$item['total']})<br>";
}

echo "<h3>各队伍得分13的次数统计</h3>";
echo "<h4>主场得分13:</h4>";
if (!empty($score13Count['home'])) {
    foreach ($score13Count['home'] as $team => $count) {
        echo "- {$team}: {$count}次<br>";
    }
} else {
    echo "无相关记录<br>";
}

echo "<h4>客场得分13:</h4>";
if (!empty($score13Count['away'])) {
    foreach ($score13Count['away'] as $team => $count) {
        echo "- {$team}: {$count}次<br>";
    }
} else {
    echo "无相关记录<br>";
}
?>

关键修改解析

  1. 得分13统计逻辑:

    • 用$score13Count二维数组分别存储主场、客场队伍的得分13次数
    • 遍历每条赛事记录时,判断得分是否为13,对应累加队伍的计数
  2. 多极值结果处理:

    • 将原有的单个极值变量改为数组,用于保存所有等于当前极值的赛事记录
    • 当遇到更大的极值时,重置数组;遇到相等极值时,将记录追加到数组中
    • 输出时用array_slice截取前5条结果,满足最多显示5条重复极值的需求

内容的提问来源于stack exchange,提问作者Mats Andersson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 17:45:37