doufu9521 2018-10-16 05:37
浏览 75

在HTML中格式化来自JSON对象的PHP数组

How can I format the following output (Using cURL in PHP to parse a json object to an array and display properly formatted text in HTML)

function curlTwitch() {

  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/kraken/streams/?game=Overwatch");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

  curl_setopt($ch, CURLOPT_HTTPHEADER, array (
    'Accept: application/vnd.twitchtv.v5+json',
    'Client-ID: x8j9dhw66qkgbsw67bfwtos4hyfeww'
  ));


$result = curl_exec($ch);

var_dump( json_decode($result, true) );

//echo json_encode($result, JSON_PRETTY_PRINT);

}

My current output fills the page in an unreadable format and is shown as something like:

array(2) { ["_total"]=> int(872) ["streams"]=> array(25) { [0]=> array(14) { 
["_id"]=> int(30799425120) ["game"]=> string(9) "Overwatch" 
["broadcast_platform"]=> string(4) "live" ["community_id"]=> string(0) "" 
["community_ids"]=> array(0) { } ["viewers"]=> int(6104) ["video_height"]=> 
int(900) ["average_fps"]=> int(60) ["delay"]=> int(0) ["created_at"]
  • 写回答

1条回答 默认 最新

  • dpw50696 2018-10-16 05:55
    关注

    If you're outputting to HTML, try adding a <pre> tag before the output:

    <pre><?= json_encode($result, JSON_PRETTY_PRINT); ?></pre>
    

    This should produce the following output:

    {
        "_total": 872,
        "streams": [
            {
                "_id": 30799425120,
                "game": "Overwatch",
                "broadcast_platform": "live",
                "community_id": '',
                "community_ids": [],
                "viewers": 6104,
                "video_height": 900,
                "average_fps": 60,
                "delay": 0,
                "created_at": '...'
            }
        ]
    }
    

    Hope that helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退