【问题标题】:PHP issue json_decode echo arrayPHP 问题 json_decode 回显数组
【发布时间】:2013-11-12 03:41:25
【问题描述】:
<?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

好的:Link goes here...

【问题讨论】:

  • print_r($array); 添加到您的代码中以查看实际解码的内容
  • 所以,我想正确的路径是$array['data']['children'][0]['data']['title']

标签: php file-get-contents json


【解决方案1】:

改用 $array['data']['children'][0]['data']['title']

【讨论】:

    【解决方案2】:

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

    试试

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      • 1970-01-01
      • 2010-10-07
      相关资源
      最近更新 更多