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 に答える