gpt4 book ai didi

php - 从 JSON 数组创建 HTML 表

转载 作者:行者123 更新时间:2023-11-28 03:21:37 26 4
gpt4 key购买 nike

几个小时以来,我一直在尝试使用 JSON 数组中的数据创建一个简单的表格。

下面是一个例子。我不知道为什么这段代码不起作用。错误是:

"Trying to get property of non-object".

$json_string = file_get_contents("https://bittrex.com/api/v1/public/getmarkethistory?market=BTC-HYPER&count=5");

$array = json_decode($json_string);
?>

<table><tr><th>
<?php foreach($array as $o): ?>

<tr>
<td><?php $o->result->TimeStamp ?></td>
</tr>
<?php endforeach; ?>
</table>

请查看 json_string 的 URL 以进行格式化。

最佳答案

或者,您可以向 json_decode($json_string, true) 添加第二个参数,使其成为数组而不是对象。考虑这个例子:

<?php
$json_string = file_get_contents("https://bittrex.com/api/v1/public/getmarkethistory?market=BTC-HYPER&count=5");
$array = json_decode($json_string, true);

?>

<table border="1" cellpadding="10">
<thead><tr><th>Timestamp</th></tr></thead>
<tbody>
<?php foreach($array['result'] as $key => $value): ?>
<tr>
<td><?php echo $value['TimeStamp']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

关于php - 从 JSON 数组创建 HTML 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24179002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com