猿问

PHP foreach 不循环遍历 JSON 文件

该foreach函数未在此 JSON 文件上运行。


我希望 PHP 遍历 JSON 文件并以 HTML 表格格式输出数据。


<?php

// Store JSON data in a PHP variable

$url = 'https://eztv.io/api/get-torrents?limit=100&page=1'; // path to your JSON file

$data = file_get_contents($url); // put the contents of the file into a variable

//var_dump(json_decode($data));

$shows = json_decode($data, true); // decode the JSON feed and make an associative array

?>

<br /><br /><br /><br />


<table>

<?php

foreach ($shows as $shows) : ?>

    <tr>

        <td> <strong><?php echo $shows["title"] . "\n"; ?></strong>  </td>

        <td> <strong><?php echo $shows["season"] . "\n"; ?></strong>  </td>

        <td> <strong><?php echo $shows["episode"] . "\n"; ?></strong>  </td>

        <td> <a href="<?php echo $shows["magnet_url"] . "\n"; ?>">magnet</a></td>

        <td> <?php echo $shows["date_released_unix"] . "\n"; ?>  </td>

        <td> <?php echo $shows["size_bytes"] . "\n"; ?>  </td>

        <td> <a href="<?php echo $shows["episode_url"] . "\n"; ?>">episode Link</a></td>

        <td> <?php echo $shows["imdb_id"] . "\n"; ?>  </td>

    </tr>

<?php endforeach; ?>

</table>

如果我运行此代码,我会Notice: Undefined index:在页面上收到错误消息。


慕的地10843
浏览 174回答 2
2回答

宝慕林4294392

torrents响应键的所有数据。您应该检查数组索引/键是否存在。<?php&nbsp;$url = 'https://eztv.io/api/get-torrents?limit=100&page=1'; // path to your JSON file$data = file_get_contents($url); // put the contents of the file into a variable$shows = json_decode($data, true); // decode the JSON feed and make an associative array&nbsp;<?php foreach ($shows['torrents'] as $shows) : ?>&nbsp; &nbsp; <tr></tr><?php endforeach; ?>

MM们

foreach ($shows as $shows) : ?>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td> <strong><?php echo $shows["title"] ?? '' . "\n"; ?></strong>&nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; <td> <strong><?php echo $shows["season"] ?? '' . "\n"; ?></strong>&nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; <td> <strong><?php echo $shows["episode"] ?? '' . "\n"; ?></strong>&nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; <td> <a href="<?php echo $shows["magnet_url"] ?? '' . "\n"; ?>">magnet</a></td>&nbsp; &nbsp; &nbsp; &nbsp; <td> <?php echo $shows["date_released_unix"] ?? '' . "\n"; ?>&nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; <td> <?php echo $shows["size_bytes"] ?? '' . "\n"; ?>&nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; <td> <a href="<?php echo $shows["episode_url"] ?? '' . "\n"; ?>">episode Link</a></td>&nbsp; &nbsp; &nbsp; &nbsp; <td> <?php echo $shows["imdb_id"] ?? '' . "\n"; ?>&nbsp; </td>&nbsp; &nbsp; </tr><?php endforeach; ?>类似于 Shivendra Singh 的回答,但我们使用空合并只是为了让事情更容易阅读。
随时随地看视频慕课网APP
我要回答