Search code examples
phpforeachjsonreddit

Reddit API - Pull Information from Json using foreach()


I am trying to pull the title and url from the Reddit JSON API using PHP's foreach() loop. Here is what the JSON outputs when used in print_r.

Here is the code I have so far...

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

Solution

  • $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'];
    }