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

PHP数组内容无法获取:比赛信息无法正常显示问题

比赛信息无法显示问题解决

页面可渲染form-group类容器,但无法展示比赛对阵内容,相关代码如下:

页面渲染代码

<form method="post">
<?php if (is_array($matches) && !empty($matches)): ?>
      <?php foreach ($matches as $match): ?>
      <div class="form-group">
      <label>
      <?php $match['Team1']['TeamName'] ?> vs <?php $match['Team2']['TeamName'] ?>
      <input type="text" class="form-control" name="tips[<?= $match['MatchID'] ?>]" required>
      </label>
      </div>
      <?php endforeach; ?>
<?php else: ?>
      <p>No matches found.</p>
<?php endif; ?>
</form>

API调用代码

function getMatchData($season, $matchday) {
    global $db;
    $apiUrl = "https://www.openligadb.de/api/getmatchdata/bl1/$season/$matchday";
    $matches = json_decode(file_get_contents($apiUrl), true);
    foreach ($matches as $match) {
        $db->insert('matches', [
            'id' => $match['MatchID'],
            'team1' => $match['Team1']['TeamName'],
            'team2' => $match['Team2']['TeamName'],
            'team1_goals' => $match['MatchResults'][0]['PointsTeam1'],
            'team2_goals' => $match['MatchResults'][0]['PointsTeam2'],
            'season' => $season,
            'matchday' => $matchday
        ]);
    }
    return $matches;
}

解决方案

自行排查后确认问题根源:数组索引大小写拼写错误。OpenLigaDB API返回的球队名称字段为小写开头的teamName,代码中误写为大写开头的TeamName,导致无法读取到对应值,因此对阵内容无法显示。

需将两处$match['Team1']['TeamName']改为$match['Team1']['teamName'],$match['Team2']['TeamName']改为$match['Team2']['teamName'],同时API插入数据库的代码中对应字段也需同步修正。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 20:35:04