Reddit API - 使用foreach()从Json中提取信息

时间:2013-05-07 04:44:28

标签: php foreach json reddit

我正在尝试使用PHP的foreach()循环从Reddit JSON API中提取标题和URL。 Here is what the JSON outputs when used in print_r.

这是我到目前为止的代码......

$string_reddit = file_get_contents("http://reddit.com/hot.json");
$json = json_decode($string_reddit, true);

1 个答案:

答案 0 :(得分:4)

$string_reddit = file_get_contents("http://reddit.com/hot.json");
$json = json_decode($string_reddit, true);  

$children = $json['data']['children'];
foreach ($children as $child){
    $title = $child['data']['title'];
    $url = $child['data']['url'];
}