Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
<?php
$string = file_get_contents("http://www.reddit.com/r/news.json");

$array = json_decode($string, true);
echo $array['data'][0]['children'][0]['data'][0]['title'][0];
?>

I've got a problem- the code doesn't echo anything.

I'm kinda new to json_decode, so any help is appreciated :-)

Edit: As a response to a comment, here's what print_r results to:

Array
(
    [kind] => Listing
    [data] => Array
        (
            [modhash] => 
            [children] => Array
                (
                    [0] => Array
                        (
                            [kind] => t3
                            [data] => Array
                                (
                                    [domain] => syracuse.com
                                    [banned_by] => 
                                    [media_embed] => Array
                                        (
                                        )

                                    [subreddit] => news
                                    [selftext_html] => 
                                    [selftext] => 
                                    [likes] => 
                                    [secure_media] => 
                                    [link_flair_text] => 
                                    [id] => 1qdtqr
                                    [secure_media_embed] => Array
                                        (
                                        )

                                    [clicked] => 
                                    [stickied] => 
                                    [author] => cadencehz
                                    [media] => 
                                    [score] => 1552
                                    [approved_by] => 
                                    [over_18] => 
                                    [hidden] => 
                                    [thumbnail] => 
                                    [subreddit_id] => t5_2qh3l
                                    [edited] => 
                                    [link_flair_css_class] => 
                                    [author_flair_css_class] => 
                                    [downs] => 978
                                    [saved] => 
                                    [is_self] => 
                                    [permalink] => /r/news/comments/1qdtqr/thousands_defend_grocery_store_employee_with/
                                    [name] => t3_1qdtqr
                                    [created] => 1384215998
                                    [url] => http://www.syracuse.com/news/index.ssf/2013/11/thousands_come_to_defense_of_clay_wegmans_employee_with_aspergers_syndrome_after.html#incart_m-rpt-2
                                    [author_flair_text] => 
                                    [title] => Thousands defend grocery store employee with Asperger's syndrome after customer yells at him for being too slow
                                    [created_utc] => 1384187198
                                    [ups] => 2530
                                    [num_comments] => 401
                                    [visited] => 
                                    [num_reports] => 
                                    [distinguished] => 
                                )

                        )

                    [1] => Array
                        (
                            [kind] => t3
                            [data] => Array
                                (
                                    [domain] => bostonherald.com
                                    [banned_by] => 
                                    [media_embed] => Array
                                        (
                                        )

                                    [subreddit] => news
                                    [selftext_html] => 
                                    [selftext] => 
                                    [likes] => 
                                    [secure_media] => 
                                    [link_flair_text] => 
                                    [id] => 1qddl6
                                    [secure_media_embed] => Array
                                        (
                                        )

                                    [clicked] => 
                                    [stickied] => 
                                    [author] => boxofrain
                                    [media] => 
                                    [score] => 2086
                                    [approved_by] => 
                                    [over_18] => 
                                    [hidden] => 
                                    [thumbnail] => 
                                    [subreddit_id] => t5_2qh3l
                                    [edited] => 
                                    [link_flair_css_class] => 
                                    [author_flair_css_class] => 
                                    [downs] => 2556
                                    [saved] => 
                                    [is_self] => 
                                    [permalink] => /r/news/comments/1qddl6/motorcycle_stolen_in_1961_found_is_recovered_and/
                                    [name] => t3_1qddl6
                                    [created] => 1384199801
                                    [url] => http://bostonherald.com/news_opinion/offbeat_news/2013/11/man_glad_stolen_motorcycle_found_after_46_years
                                    [author_flair_text] => 
                                    [title] => Motorcycle stolen in 1961 found is recovered and will be returned to it 73 year old owner.
                                    [created_utc] => 1384171001
                                    [ups] => 4642
                                    [num_comments] => 141
                                    [visited] => 
                                    [num_reports] => 
                                    [distinguished] => 
                                )

                        )

                    [2] => Array
                        (
                            [kind] => t3
                            [data] => Array
                                (
                                    [domain] => hosted.ap.org
                                    [banned_by] => 
                                    [media_embed] => Array
                                        (
                                        )

                                    [subreddit] => news
                                    [selftext_html] => 
                                    [selftext] => 
                                    [likes] => 
                                    [secure_media] => 
                                    [link_flair_text] => 
                                    [id] => 1qe6gp
                                    [secure_media_embed] => Array
                                        (
                                        )

                                    [clicked] => 
                                    [stickied] => 
                                    [author] => donkey-kick
                                    [media] => 
                                    [score] => 415
                                    [approved_by] => 
                                    [over_18] => 
                                    [hidden] => 
                                    [thumbnail] => 
                                    [subreddit_id] => t5_2qh3l
                                    [edited] => 
                                    [link_flair_css_class] => 
                                    [author_flair_css_class] => 
                                    [downs] => 334
                                    [saved] => 
                                    [is_self] => 
                                    [permalink] => /r/news/comments/1qe6gp/atheist_mega_churches_take_hold_in_the_us_and/
                                    [name] => t3_1qe6gp
                                    [created] => 1384224670
                                    [url] => http://hosted.ap.org/dynamic/stories/U/US_ATHEIST_MEGACHURCH?SITE=AP&amp;SECTION=HOME&amp;TEMPLATE=DEFAULT&amp;CTIME=2013-11-10-17-03-15
                                    [author_flair_text] => 
                                    [title] => Atheist Mega Churches take hold in the US and around the world.
                                    [created_utc] => 1384195870
                                    [ups] => 749
                                    [num_comments] => 368
                                    [visited] => 
                                    [num_reports] => 
                                    [distinguished] => 
                                )

                        )

                    [3] => Array
                        (
                            [kind] => t3
                            [data] => Array
                                (
                                    [domain] => abcnews.go.com
                                    [banned_by] => 
                                    [media_embed] => Array
                                        (
                                        )

                                    [subreddit] => news
                                    [selftext_html] => 
                                    [selftext] => 
                                    [likes] => 
                                    [secure_media] => 
                                    [link_flair_text] => 
                                    [id] => 1qdie4
                                    [secure_media_embed] => Array
                                        (
                                        )

                                    [clicked] => 
                                    [stickied] => 
                                    [author] => Hoosier_made
                                    [media] => 
                                    [score] => 984
                                    [approved_by] => 
                                    [over_18] => 
                                    [hidden] => 
                                    [thumbnail] => 
                                    [subreddit_id] => t5_2qh3l
                                    [edited] => 
                                    [link_flair_css_class] => 
                                    [author_flair_css_class] => 
                                    [downs] => 400
                                    [saved] => 
                                    [is_self] => 
                                    [permalink] => /r/news/comments/1qdie4/abc_news_amy_robach_has_mammogram_live_on_gma/
                                    [name] => t3_1qdie4
                                    [created] => 1384206209
                                    [url] => http://abcnews.go.com/blogs/health/2013/11/11/abc-news-amy-robach-reveals-breast-cancer-diagnosis/
                                    [author_flair_text] => 
                                    [title] => ABC News’ Amy Robach has mammogram live on GMA. Results Reveal Breast Cancer Diagnosis.
                                    [created_utc] => 1384177409
                                    [ups] => 1384
                                    [num_comments] => 130
                                    [visited] => 
                                    [num_reports] => 
                                    [distinguished] => 
                                )

                        )

                  // ECT... I CUT HERE BECAUSE IT WENT ON FOR A WHILE

            [after] => t3_1qdinr
            [before] => 
        )

)

Here's the url to the exact output, I had to trim the code to fit the character limit so maybe there's something I messed up ! :P

Okay: Link goes here...

share|improve this question
4  
add print_r($array); to your code to see what is actually decoded –  Lashane Nov 12 '13 at 3:44
 
Done @IlyaBursov :) –  dfdf Nov 12 '13 at 3:50
1  
so, proper path I suppose will be $array['data']['children'][0]['data']['title'] –  Lashane Nov 12 '13 at 3:53
add comment

2 Answers

try $array['data']['children'][0]['data']['title'] instead

share|improve this answer
 
Thanks! That fixes it :D –  dfdf Nov 12 '13 at 3:54
add comment

the first 'data' is an object, not an array, and will be decoded to an associative array in php. There is no key '0' for that array. There are similar issues later in the keys you're trying.

Try

echo  $array['data']['children'][0]['data']['title']
share|improve this answer
 
Thanks! That fixes + explains it :D –  dfdf Nov 12 '13 at 3:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.