使用 PHP 读取和写入包含锦标赛比赛结果的文本文件

Reading and writing a text file with the results of tournament matches with PHP

提问人:kalawaja 提问时间:10/10/2022 更新时间:10/10/2022 访问量:81

问:

欧洲冠军联赛G组足球比赛结果:

RB莱比锡;摩纳哥;平局
波尔图;贝西克塔斯JK;损失
贝西克塔斯JK;RB莱比锡;赢得
摩纳哥;波尔图足球俱乐部;损失
AS 摩纳哥;贝西克塔斯JK;负
RB莱比锡;波尔图足球俱乐部;赢得
贝西克塔斯JK;摩纳哥;平局
波尔图;RB莱比锡;赢得
贝西克塔斯JK;波尔图足球俱乐部;绘制
摩纳哥;RB莱比锡;负
FC Porto;AS Monaco;赢得
RB莱比锡;贝西克塔斯JK;损失

  1. 比赛结果以所列球队为准。

    (例如:
    贝西克塔斯JK;RB莱比锡;赢

    • 意味着贝西克塔斯JK击败了RB莱比锡。

    摩纳哥足球俱乐部;贝西克塔斯JK;损失

    • 意味着贝西克塔斯击败了摩纳哥足球俱乐部。

    RB莱比锡;摩纳哥足球俱乐部;画

    • 意味着RB莱比锡和摩纳哥足球俱乐部打成平手。)
  2. 获胜可为球队赢得 3 分。平局获得 1。亏损赚 0。

  3. 结果应按点降序排列。如果出现平局,则按字母顺序排列球队。

输出应如下所示:

Team                           | MP |  W |  D |  L |  P
Besiktas JK                    |  6 |  4 |  2 |  0 |  14
FC Porto                       |  6 |  3 |  1 |  2 |  10
RB Leipzig                     |  6 |  2 |  1 |  3 |  6
AS Monaco                      |  6 |  0 |  2 |  4 |  2

但是,我无法获得排名和客场胜利的结果。我该怎么做?

#tournament.txt

RB Leipzig;AS Monaco;draw
FC Porto;Besiktas JK;loss
Besiktas JK;RB Leipzig;win
AS Monaco;FC Porto;loss
AS Monaco;Besiktas JK;loss
RB Leipzig;FC Porto;win
Besiktas JK;AS Monaco;draw
FC Porto;RB Leipzig;win
Besiktas JK;FC Porto;draw
AS Monaco;RB Leipzig;loss
FC Porto;AS Monaco;win
RB Leipzig;Besiktas JK;loss
#tournament.php

<?php

$lines = file('tournament.txt');

foreach ($lines as $line) {
    
    $parts = explode(';', $line);
    
    $teams[$parts[0]][] = $parts[2];
    $teams[$parts[1]][] = $parts[2];
}


uksort($teams, function ($a, $b) use ($teams) {
    $aPoints = 0;
    $bPoints = 0;
    foreach ($teams[$a] as $result) {
        if ($result == 'win') {
            $aPoints += 3;
        } elseif ($result == 'draw') {
            $aPoints += 1;
        }
    }
    foreach ($teams[$b] as $result) {
        if ($result == 'win') {
            $bPoints += 3;
        } elseif ($result == 'draw') {
            $bPoints += 1;
        }
    }
    foreach ($teams[$a] as $result) {
        if ($result == 'loss') {
            $aPoints += 0;
            $bPoints += 3;
        }
    }
    foreach ($teams[$b] as $result) {
        if ($result == 'loss') {
            $aPoints += 3;
            $bPoints += 0;
        }
    }
    if ($aPoints == $bPoints) {
        return $a <=> $b;
    }
    return $bPoints <=> $aPoints;
});

$fp = fopen('tournament.txt', 'w');
fwrite($fp, "Team                          | MP |  W |  D |  L |  P

");
foreach ($teams as $team => $results) {
    $mp = count($results);
    $w = 0;
    $d = 0;
    $l = 0;
    foreach ($results as $result) {
        if ($result == 'win') {
            $w++;
        } elseif ($result == 'draw') {
            $d++;
        } else {
            $l++;
        }
    }
    $p = $w * 3 + $d;
    fwrite($fp, sprintf("%-30s| %2d | %2d | %2d | %2d | %2d

", $team, $mp, $w, $d, $l, $p));
}

fclose($fp);

?>
php fopen 爆炸 fclose

评论

0赞 Juan 10/10/2022
还有你展示的那个代码......?什么给了...?问题是什么..?
0赞 Martin 10/10/2022
当数据库可以使所有这些变得如此简单时,为什么还要使用文本文件?
0赞 kalawaja 10/11/2022
我尝试处理文本文件。是的,我同意:数据库会让它变得更简单。但是,我不得不尝试一下。@Martin

答:

1赞 Juan 10/10/2022 #1

我无法测试您的代码,因为我使用的是 PHP 版本 5.6.36。 但是使用下面从 PHP 5 工作的代码,我得到了结果。 包括客场胜利。

<style type="text/css">
table       { border-spacing: 0px; }
table th    { padding: 5px; border: 1px solid black; }
table td    { padding: 3px; border: 1px solid dimgrey; }
</style>


<?
// Initialisation
$infoList = array("MP","W","Wext","D","L","P");

// Open File
$lines = file('tournament.txt');

// For Each Line
foreach ($lines as $line)
{
  $parts = explode(';', $line);
  
  // Extraction
  $teamA    = $parts[0];
  $teamB    = $parts[1];
  $score    = $parts[2];

  // Initialization
  $teamList["$teamA"]["W"]      = 0;
  $teamList["$teamA"]["Wext"] = 0;
  $teamList["$teamA"]["L"]      = 0;
  $teamList["$teamA"]["D"]      = 0;
  $teamList["$teamA"]["P"]      = 0;
  $teamList["$teamA"]["MP"]     = (array_key_exists("MP", $teamList["$teamA"]))?bcadd($teamList["$teamA"]["MP"],1,0):1;

  // Initialization
  $teamList["$teamB"]["W"]      = 0;
  $teamList["$teamB"]["Wext"] = 0;
  $teamList["$teamB"]["L"]      = 0;
  $teamList["$teamB"]["D"]      = 0;
  $teamList["$teamB"]["P"]      = 0;
  $teamList["$teamB"]["MP"]     = (array_key_exists("MP", $teamList["$teamB"]))?bcadd($teamList["$teamB"]["MP"],1,0):1;

  // Memorisation
  $matchList[] = array("teamA"=>$teamA, "teamB"=>$teamB, "score"=>trim($score));
}
// End - For Each Line



// For Each Match
foreach($matchList as $matchKey => $matchValue)
{
    // If Team A Win
    if($matchValue["score"]=="win")
    {
        // Memorisation Team A
        $teamList["".$matchValue["teamA"].""]["W"]  = bcadd($teamList["".$matchValue["teamA"].""]["W"],1,0);
        $teamList["".$matchValue["teamA"].""]["P"]  = bcadd($teamList["".$matchValue["teamA"].""]["P"],3,0);
    }


    // If Team A Loss
    if($matchValue["score"]=="loss")
    {
        // Memorisation Team B
        $teamList["".$matchValue["teamB"].""]["W"]      = bcadd($teamList["".$matchValue["teamB"].""]["W"],1,0);
        $teamList["".$matchValue["teamB"].""]["Wext"] = bcadd($teamList["".$matchValue["teamB"].""]["Wext"],1,0);
        $teamList["".$matchValue["teamB"].""]["P"]      = bcadd($teamList["".$matchValue["teamB"].""]["P"],3,0);
    }


    // If Equality
    if($matchValue["score"]=="draw")
    {
        // Memorisation Team A
        $teamList["".$matchValue["teamA"].""]["D"]  = bcadd($teamList["".$matchValue["teamA"].""]["D"],1,0);
        $teamList["".$matchValue["teamA"].""]["P"]  = bcadd($teamList["".$matchValue["teamA"].""]["P"],1,0);

        // Memorisation Team B
        $teamList["".$matchValue["teamB"].""]["D"]  = bcadd($teamList["".$matchValue["teamB"].""]["D"],1,0);
        $teamList["".$matchValue["teamB"].""]["P"]  = bcadd($teamList["".$matchValue["teamB"].""]["P"],1,0);
    }
}
// Fin - For Each Match










// -------- Display in HTML -------- //

echo "<table>";
echo "<tr>";
echo "<th></th>";
foreach($infoList as $infoKey => $infoValue)    { echo "<th>".$infoValue."</th>"; }
echo "</tr>";


// For Each Team
foreach($teamList as $teamName => $teamValue)
{
    echo "<tr>";
    echo "<td>".$teamName."</td>";

    // For Each Type Information
    foreach($infoList as $infoKey => $infoValue)
    {
        echo "<td>".$teamValue["$infoValue"]."</td>";
    }
    // End - For Each Type Information

    echo "</tr>";
}
// End - For Each Team

echo "</table>";

// --------------------------------- //
?>

它可能看起来有点重,但它允许有一个包含所有必要信息的 teamList 数组

评论

1赞 Martin 10/10/2022
在使用 PHP 7.4 或更高版本帮助自己之前,你真的应该停止在这里帮助他人。PHP 5 是将近 4 年前的生命周期结束!!更新!!!
0赞 Juan 10/10/2022
我不是我开发的网络的管理员。在家里,我使用的是PHP 7.X。但我不在家,我利用工作休息时间试图帮助别人。与我所在的网络以及我不管理的网络。