再帰配列の印刷 - json

誰かがこのjsonデコードされた配列から「テキスト」値を出力するのを手伝ってもらえますか? $ obj ["text"]をエコーし​​ましたが、空白になります:(

ああ、私はvar_dumpをしました;それはその配列を言います1要素の配列内の20要素:-) したがって、コードを42回編集した後、すばらしいFangelがテキストを印刷するのに役立ったという理由だけで、次の行を配置する必要があります。 echo $ obj [0] ["text"];

$obj=[
{
"created_at":"Mon Sep 03 05:00:30 +0000 2012",
"id":242487207418544128,
"id_str":"242487207418544128",
"text":"Clint, come to the Democratic Convention. We"ll get you a coherent speech to read - and we"ll even help you comb your hair.",
"source":"web",
"truncated":false,
"in_reply_to_status_id":null,
"in_reply_to_status_id_str":null,
"in_reply_to_user_id":null,
"in_reply_to_user_id_str":null,
"in_reply_to_screen_name":null,
"user":{
"id":15376626,
"id_str":"15376626",
"name":"BarrackObama",
"screen_name":"BarrackObama",
"location":"Washington, D.C.",
"url":null,
"description":"President of the United States of America",
"protected":false,
"followers_count":94289,
"friends_count":1,
"listed_count":577,
"created_at":"Thu Jul 10 12:05:37 +0000 2008",
"favourites_count":0,
"utc_offset":-18000,
"time_zone":"Quito",
"geo_enabled":false,
"verified":false,
"statuses_count":106,
"lang":"en",
"contributors_enabled":false,
"is_translator":false,
"profile_background_color":"E6EB6F",
"profile_background_image_url":"http://a0.twimg.com/profile_background_images/76798997/PresidentialSeal.jpg",
"profile_background_image_url_https":"https://si0.twimg.com/profile_background_images/76798997/PresidentialSeal.jpg",
"profile_background_tile":false,
"profile_image_url":"http://a0.twimg.com/profile_images/56441335/so_normal.jpg",
"profile_image_url_https":"https://si0.twimg.com/profile_images/56441335/so_normal.jpg",
"profile_link_color":"0FA7FF",
"profile_sidebar_border_color":"EAFF08",
"profile_sidebar_fill_color":"171CA6",
"profile_text_color":"E69407",
"profile_use_background_image":true,
"default_profile":false,
"default_profile_image":false,
"following":null,
"follow_request_sent":null,
"notifications":null
},
"geo":null,
"coordinates":null,
"place":null,
"contributors":null,
"retweet_count":110,
"entities":{
"hashtags":[

],
"urls":[

],
"user_mentions":[

]
},
"favorited":false,
"retweeted":false
}
]

回答:

回答№1は0

http://php.net/manual/en/function.json-decode.php

アソーク

TRUEの場合、返されたオブジェクトは連想に変換されます 配列。

2番目のパラメータをtrueに設定する場合は、次のことを行う必要があります。 echo $obj["text"]; の代わりに $obj->text; なぜなら、マニュアルの2番目のパラメーターで述べられているように、trueの場合、JSONのオブジェクトであっても、json_decodeが連想配列を返すように強制するからです。

私が見る限り、JSONにはオブジェクトの配列があります。したがって、jsonデコード後、オブジェクトの配列も必要になります。使用するだけ foreach 配列を調べてすべてのアイテムのテキストを出力するループ:

foreach($obj as $item) {
echo $item->text + "<br/>";
}

または連想配列の場合はこのようになります(2番目のパラメータが json_decode 本当です):

foreach($obj as $item) {
echo $item["text"] + "<br/>";
}

回答№2の場合は0

これはそれを印刷する方法です:echo $obj[0]["text"];