【问题标题】:How can i parse json to a html table using PHP?如何使用 PHP 将 json 解析为 html 表?
【发布时间】:2016-05-10 03:35:06
【问题描述】:

我有这样的json文件

[{
    "Shop_name": "916",
    "Shop_id": "916TCR",
    "Address":"cdsasffafa"
    "numbers": "4",
    "mob_no": "9447722856"
}, {
    "Shop_name": "Chicking",
    "Shop_id": "CKGTCR",
    "Address":"afagagg",
    "numbers": "8",
    "mob_no": "6767564532"
}]

我想将其视为 HTML 表格,其中行标题为 Shop_name、Shop_id、Address 和下一行的数据。

我尝试对其进行解码并显示为表格 (How can i parse json into a html table using PHP?)。 但是这里的 Json 文件格式不同。 也想知道 Json 文件是否会随着 [0],[1],[2]....

变大

【问题讨论】:

  • 我认为带有奇怪双引号的 JSON 不是有效的
  • 更正 JSON !@RiggsFolly
  • 所以现在字符集是固定的,只是做一些编码的情况
  • 请显示您实际用于尝试使其工作的实际代码

标签: php html json html-table


【解决方案1】:

您应该将 json 数据解析为 php 对象,而不是像下面那样迭代数据;

$myData = <<<JSON

[
{"Shop_name":"minh",
  "Shop_id":"916TCR",
  "Address":"cdsasffafa",
  "numbers":"4",
  "mob_no":"9447722856"
},
{"Shop_name":"Chig",
 "Shop_id":"CKGTCR",
 "Address":"afagagg",
 "numbers":"8",
 "mob_no":"6767564532"
}
]

JSON;

$myObject = json_decode($myData);

?>
<table>
<tr>
    <td>Shop Name</td>
    <td>Shop ID</td>
    <td>Address</td>
    <td>Numbers</td>
    <td>Mob No</td>
</tr>
<?PHP
foreach($myObject as $key=>$item)
{
    ?>
    <tr>
        <td><?PHP echo $item->Shop_name; ?></td>
        <td><?PHP echo $item->Shop_id; ?></td>
        <td><?PHP echo $item->Address; ?></td>
        <td><?PHP echo $item->numbers; ?></td>
        <td><?PHP echo $item->mob_no; ?></td>
    </tr>
    <?PHP
}
?>
</table>

这里有一个工作示例:http://ideone.com/IqZLMs

PS:你应该把这个“引用”改成“

【讨论】:

    猜你喜欢
    • 2014-11-30
    • 2013-11-15
    • 2021-09-18
    • 2015-09-19
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多