PHP问题json_decode echo数组

时间:2013-11-12 03:41:26

标签: php file-get-contents json

<?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];
?>

我遇到了问题 - 代码没有回应任何问题。

我是json_decode的新手,所以感谢任何帮助: - )

编辑:作为对评论的回复,以下是print_r的结果:

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] => 
        )

)

这是确切输出的网址,我不得不修剪代码以适应字符限制,所以也许我搞砸了! :P

好的:Link goes here...

2 个答案:

答案 0 :(得分:2)

尝试$ array ['data'] ['children'] [0] ['data'] ['title']而不是

答案 1 :(得分:1)

第一个'data'是一个对象,而不是一个数组,并将被解码为php中的一个关联数组。该阵列没有键'0'。您正在尝试的密钥中存在类似的问题。

尝试

echo  $array['data']['children'][0]['data']['title']