【问题标题】:JSON to HTML table or div using PHP使用 PHP 将 JSON 转换为 HTML 表或 div
【发布时间】:2018-09-23 14:17:24
【问题描述】:

我从 API 获得一些 JSON 响应,我想将此响应显示到 HTML 表或 DIV 中。我已经使用以下行解码了响应:

$data = json_decode($response);

我在$data 变量中得到以下输出。我需要帮助将以下响应显示到表格中,请提出建议。

stdClass Object (
    [response] => stdClass Object (
    [result] => stdClass Object (
        [Leads] => stdClass Object (
            [row] => array (
                [0] => stdClass Object (
                    [no] => 1
    [FL] => array (
        [0] => stdClass Object (
            [val] => LEADID
    [content] => 1852733000019035100
    )
    [1] => stdClass Object (
        [val] => SMOWNERID
    [content] => 1852733000016827001
    )

【问题讨论】:

  • 欢迎来到 SO!请您先阅读How to create a Minimal, Complete, and Verifiable example 好吗?如果您的问题更详细,也许我们可以给您一个正确的答案。
  • 感谢您的关注@fabrik 我用以下脚本解决了这个问题:<tbody> <?php foreach ($data->response->result->Leads->row as $row) { $columns = 0; echo '<tr>'; foreach ($row->FL as $fl) { echo '<td class="value">'; echo $fl->content; echo '</td>'; } echo '</tr>'; } ?> </tbody>

标签: php json html-table


【解决方案1】:

以下代码对我来说很神奇,发布以防万一它可以帮助其他具有相同 JSON 结构的人。

<tbody>
<?php
            foreach ($data->response->result->Leads->row as $row) {
                echo '<tr>';
                foreach ($row->FL as $fl) {
                    echo '<td class="value">';
                    echo $fl->content;
                    echo '</td>';
                }
                echo '</tr>';
            }
            ?>
</tbody> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 2019-05-07
    • 1970-01-01
    • 2010-12-30
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多