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

)

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

好的:链接到这里...

4

2 回答 2

2

尝试 $array['data']['children'][0]['data']['title'] 代替

于 2013-11-12T03:48:43.313 回答
1

第一个“数据”是一个对象,而不是一个数组,它将被解码为 php 中的关联数组。该数组没有键“0”。稍后在您尝试的键中也有类似的问题。

尝试

echo  $array['data']['children'][0]['data']['title']
于 2013-11-12T03:49:08.277 回答