用PHP读写一个包含锦标赛比赛结果的文本文件



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

RB莱比锡;AS摩纳哥;战平波尔图足球俱乐部;Besiktas JK;损失
Besiktas JK;莱比锡RB;赢得
AS摩纳哥;波尔图足球俱乐部;损失
AS摩纳哥;Besiktas JK;亏损
莱比锡RB;波尔图足球俱乐部;获胜
Besiktas JK;AS摩纳哥;战平波尔图足球俱乐部;莱比锡RB;获胜
Besiktas JK;波尔图足球俱乐部;抽签
AS摩纳哥;莱比锡RB;输给波尔图足球俱乐部;AS摩纳哥;赢得莱比锡RB;Besiktas JK;损失

  1. 比赛结果指的是列出的第一支球队。

    (示例:
    贝西克塔斯JK;RB莱比锡;获胜

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

    AS摩纳哥足球俱乐部;Besiktas JK;损失

    • 意味着贝西克塔斯队击败了AS摩纳哥队

    RB莱比锡;AS摩纳哥足球俱乐部;绘制

    • 意味着莱比锡RB和摩纳哥AS打平(
  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 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数组

相关内容

  • 没有找到相关文章

最新更新