【问题标题】:How to convert Json decode to HTML table如何将 Json 解码转换为 HTML 表
【发布时间】:2021-06-05 01:24:59
【问题描述】:

我有一个输出一些 json 的脚本。我的 json 看起来像:我正在尝试获取 twitter 趋势并显示到我的网站。

Array
(
    [0] => Array
        (
            [trends] => Array
                (
                    [0] => Array
                        (
                            [name] => #Allah_In_Quran
                            [url] => http://twitter.com/search?q=%23Allah_In_Quran
                            [promoted_content] => 
                            [query] => %23Allah_In_Quran
                            [tweet_volume] => 216174
                        )

                    [1] => Array
                        (
                            [name] => Last Prophet Saint Rampal Ji
                            [url] => http://twitter.com/search?q=%22Last+Prophet+Saint+Rampal+Ji%22
                            [promoted_content] => 
                            [query] => %22Last+Prophet+Saint+Rampal+Ji%22
                            [tweet_volume] => 161376
                        )

                    [2] => Array
                        (
                            [name] => BBB3 SHOOT WRAP UP
                            [url] => http://twitter.com/search?q=%22BBB3+SHOOT+WRAP+UP%22
                            [promoted_content] => 
                            [query] => %22BBB3+SHOOT+WRAP+UP%22
                            [tweet_volume] => 224067
                        )

                    [3] => Array
                        (
                            [name] => PROUD OF LOUIS
                            [url] => http://twitter.com/search?q=%22PROUD+OF+LOUIS%22
                            [promoted_content] => 
                            [query] => %22PROUD+OF+LOUIS%22
                            [tweet_volume] => 336126
                        )

                    [4] => Array
                        (
                            [name] => #MaiBhiTiranga
                            [url] => http://twitter.com/search?q=%23MaiBhiTiranga
                            [promoted_content] => 
                            [query] => %23MaiBhiTiranga
                            [tweet_volume] => 28770
                        )

                    [5] => Array
                        (
                            [name] => #BURARS
                            [url] => http://twitter.com/search?q=%23BURARS
                            [promoted_content] => 
                            [query] => %23BURARS
                            [tweet_volume] => 36755
                        )

                    [6] => Array
                        (
                            [name] => LOUIS TOMLINSON
                            [url] => http://twitter.com/search?q=%22LOUIS+TOMLINSON%22
                            [promoted_content] => 
                            [query] => %22LOUIS+TOMLINSON%22
                            [tweet_volume] => 342661
                        )

                    [7] => Array
                        (
                            [name] => Rudrakaal Tomorrow
                            [url] => http://twitter.com/search?q=%22Rudrakaal+Tomorrow%22
                            [promoted_content] => 
                            [query] => %22Rudrakaal+Tomorrow%22
                            [tweet_volume] => 
                        )

                    [8] => Array
                        (
                            [name] => #TeamIndia
                            [url] => http://twitter.com/search?q=%23TeamIndia
                            [promoted_content] => 
                            [query] => %23TeamIndia
                            [tweet_volume] => 60971
                        )

                    [9] => Array
                        (
                            [name] => #SSMBPrideOfTollywood
                            [url] => http://twitter.com/search?q=%23SSMBPrideOfTollywood
                            [promoted_content] => 
                            [query] => %23SSMBPrideOfTollywood
                            [tweet_volume] => 72219
                        )

                    [10] => Array
                        (
                            [name] => Xhaka
                            [url] => http://twitter.com/search?q=Xhaka
                            [promoted_content] => 
                            [query] => Xhaka
                            [tweet_volume] => 45763
                        )

                    
            [as_of] => 2021-03-06T15:20:54Z
            [created_at] => 2021-03-06T03:44:12Z
            [locations] => Array
                (
                    [0] => Array
                        (
                            [name] => India
                            [woeid] => 23424848
                        )

                )

        )

)

我使用下面的代码获取输出

$getfield = '?id=23424848';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
echo "<pre>";
print_r($string);
echo "</pre>";

我已尝试使用 foreach 循环,但无法从 jason 获取数据。我想把这个 jason 数据转换成表格格式。

【问题讨论】:

  • 您在寻找什么样的表格格式?
  • 我正在寻找表格格式“排名”、“推特热门话题”、“推文量”。这 3 个数据在 json 输出中可用。
  • 能否在json编码前将json数据作为字符串给出
  • 我试过了,但没用。这里 Json 数组数据显示正确。我们必须将此数据转换为 HTML 表格或 Datatable 以在前端显示数据的表格格式。
  • 你写“我的 json 看起来像”并向我们展示了一个数组。虽然您已经解码了 JSON 数据,但很容易对其进行迭代并为表格生成 HTML 标记。

标签: php html json twitter


【解决方案1】:

试试下面的循环:

echo "<pre><table>";
echo "<tr><th>name</th><th>url</th></tr>";
foreach ($datas as $data) {
    $trends = $data['trends'];
    foreach ($trends as $tweet) {
        echo "<tr>";
        echo "<td>".$tweet['name']."</td>";
        echo "<td>".$tweet['url']."</td>";
        echo "</tr>";
    }
}
echo "</table></pre>";

前提是 json 解码工作正常。演示代码在这里:https://ideone.com/NfHmiJ

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 2013-09-03
    • 2014-02-23
    • 2020-06-13
    • 2014-10-28
    • 2018-10-04
    • 2015-09-02
    • 2013-07-02
    相关资源
    最近更新 更多