解析JSON并使用PHP循环它

时间:2015-01-04 13:27:13

标签: php json

我的问题是将JSON文件加载到我的php页面中。使用XML / RSS我没有问题,并且在javascript中我成功了,但对于这个学校项目,我需要在PHP中加载JSON数据。

{"tvshows":[
      {
         "showname":"Parenthood ",
         "date":"2014-12-28",
         "fullTitle":"Parenthood (2010) 2x10 \"Happy Thanksgiving \"",
         "img":"http:\/\/zapp.trakt.us\/images\/episodes\/117-2-10.jpg?4"
      },
      {
         "showname":"Parenthood ",
         "date":"2014-12-28",
         "fullTitle":"Parenthood (2010) 2x11 \"Damage Control\"",
         "img":"http:\/\/zapp.trakt.us\/images\/episodes\/117-2-11.jpg?4"
      },
      {
         "showname":"Benched ",
         "date":"2014-12-27",
         "fullTitle":"Benched 1x09 \"A New Development\"",
         "img":"http:\/\/zapp.trakt.us\/images\/episodes\/33221-1-9.jpg?3"
      }
   ]
}

当我使用这个PHP代码时看不到输出:

$string = file_get_contents("url");
$json = json_decode($string, true);

print_r($json);

之后我想浏览电视节目并显示showame值。我该怎么做?

1 个答案:

答案 0 :(得分:0)

foreach($json["tvshows"] as $key=>$val){
    echo $key."-".$val["showname"]."<br/>";
}