在phptable中显示json内容

Display json content in php table

我试图在 php table 中显示 json 内容,但每次都出现错误。我有一些语法错误,无法弄清楚我应该更改什么?

PS。尝试使用 Slim 框架构建它

这是我的代码:

<div class="data-table-wrapper">
<?php
    $myData = file_get_contents("http://ergast.com/api/f1/current.json");
    $myObject = json_decode($myData);
?>
    <table class="data-table">
        <thead>
            <tr>
                <td>Date</td>
                <td>Time</td>
                <td>Round</td>
                <td>Circuit</td>
                <td>Location</td>
            </tr>
        </thead>
        <?PHP
        foreach($myObject as $key=>$item);
        ?>
        <tr>
            <td><?PHP echo $item->Date; ?></td>
            <td><?PHP echo $item->Time; ?></td>
            <td><?PHP echo $item->Round; ?></td>
            <td><?PHP echo $item->Circuit; ?></td>
            <td><?PHP echo $item->Location; ?></td>
        </tr>
        <?PHP
            }
        ?>
    </table>
</div>

我的错误是:

注意:未定义 属性:C:\xampp\htdocs\challenge\app\view\challenge.php 中的 stdClass::$Date 第 38 行

注意:未定义 属性:C:\xampp\htdocs\challenge\app\view\challenge.php 中的 stdClass::$Time 第 39 行

注意:未定义 属性:C:\xampp\htdocs\challenge\app\view\challenge.php 中的 stdClass::$Round 第 40

注意:未定义 属性:C:\xampp\htdocs\challenge\app\view\challenge.php 中的 stdClass::$Circuit 第 41 行

问题是解码后的数组看起来不一样。在循环之前转储编码数组以了解正确的数据结构或使用 JSON beautify service。也使用右边的upper/lowercase来处理属性。更新的循环可能如下所示:

<div class="data-table-wrapper">
<?php
$myData = file_get_contents("http://ergast.com/api/f1/current.json");
$myObject = json_decode($myData);

?>
   <table class="data-table">
   <thead>
    <tr>
     <td>Date</td>
      <td>Time</td>
      <td>Round</td>
      <td>Circuit</td>
      <td>Location</td>
    </tr>
    <?PHP
    foreach($myObject->MRData->RaceTable->Races as $key=>$item){
    ?>
  <tr>
    <td><?PHP echo $item->date; ?></td>
    <td><?PHP echo $item->time; ?></td>
    <td><?PHP echo $item->round; ?></td>
    <td><?PHP echo $item->Circuit->circuitName; ?></td>
    <td><?PHP echo $item->Circuit->Location->locality; ?></td>
  </tr>
 <?PHP
   }
 ?>
   </table>
       </div>
        </div>

我刚刚将您的 json 格式与代码进行了比较。 json 的路径映射不正确。我已在下方更正;

请查看以下内容:

PHP代码:

$myData = file_get_contents("http://ergast.com/api/f1/current.json");
$myObject = json_decode($myData);
$myObjectMap = $myObject->MRData->RaceTable->Races;

每种格式:

  <?php foreach($myObjectMap as $key => $item): ?>
    <tr>
      <td><?PHP echo $item->date; ?></td>
      <td><?PHP echo $item->time; ?></td>
      <td><?PHP echo $item->round; ?></td>
      <td><?PHP echo $item->Circuit->circuitId; ?></td>
      <td><?PHP echo $item->Circuit->Location->country; ?></td>
    </tr>
  <?php endforeach; ?>

完整代码:

<html>
<head>
  <title>PHP</title>
</head>
<body>
  <?php
    $myData = file_get_contents("http://ergast.com/api/f1/current.json");
    $myObject = json_decode($myData);
    $myObjectMap = $myObject->MRData->RaceTable->Races;
  ?>
  <table>
    <thead>
      <tr>
        <td>Date</td>
        <td>Time</td>
        <td>Round</td>
        <td>Circuit</td>
        <td>Location</td>
      </tr>
    </thead>
    <tbody>
      <?php foreach($myObjectMap as $key => $item): ?>
        <tr>
          <td><?PHP echo $item->date; ?></td>
          <td><?PHP echo $item->time; ?></td>
          <td><?PHP echo $item->round; ?></td>
          <td><?PHP echo $item->Circuit->circuitId; ?></td>
          <td><?PHP echo $item->Circuit->Location->country; ?></td>
        </tr>
      <?php endforeach; ?>
    </tbody>
  </table>

</body>
</html>