【问题标题】:Reddit API - Pull Information from Json using foreach()Reddit API - 使用 foreach() 从 Json 中提取信息
【发布时间】:2013-05-07 04:44:28
【问题描述】:

我正在尝试使用 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);

【问题讨论】:

    标签: php foreach json reddit


    【解决方案1】:
    $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'];
    }
    

    【讨论】:

    • 我添加了 echo($title);回声“
      ”;它似乎只输出 .json 中的第一个和最后一个条目。
    猜你喜欢
    • 1970-01-01
    • 2020-11-16
    • 2012-01-01
    • 2012-08-09
    • 2015-06-02
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2013-12-03
    相关资源
    最近更新 更多