Aderus
@Aderus

Как добраться до вложенного массива из ответа twitter api?

Здравствуйте! После запроса, мне пришел ответ. Я его декодировал и теперь он выглядит так:
Array ( [statuses] => Array ( [0] => Array ( [created_at] => Thu Jan 05 17:37:27 +0000 2017 [id] => 817062470183505925 [id_str] => 817062470183505925 [text] => RT @ErikGriffin: Thank @GAEMS_PGE road life is so much better now! Got my @DestinyTheGame and room service. #nerd https://t.co/P7frCJDZu1 [truncated] => [entities] => Array ( [hashtags] => Array ( [0] => Array ( [text] => nerd [indices] => Array ( [0] => 108 [1] => 113 ) ) ) [symbols] => Array ( ) [user_mentions] => Array ( [0] => Array ( [screen_name] => ErikGriffin [name] => Erik Griffin [id] => 16053405 [id_str] => 16053405 [indices] => Array ( [0] => 3 [1] => 15 ) ) [1] => Array ( [screen_name] => GAEMS_PGE [name] => GAEMS [id] => 148112305 [id_str] => 148112305 [indices] => Array ( [0] => 23 [1] => 33 ) ) [2] => Array ( [screen_name] => DestinyTheGame [name] => Destiny The Game [id] => 948444535 [id_str] => 948444535 [indices] => Array ( [0] => 74 [1] => 89 ) ) ) [urls] => Array ( ) [media] => Array ( [0] => Array ( [id] => 817054192359456768 [id_str] => 817054192359456768 [indices] => Array ( [0] => 114 [1] => 137 ) [media_url] => http://pbs.twimg.com/media/C1bCdYHUQAAKEwc.jpg [media_url_https] => https://pbs.twimg.com/media/C1bCdYHUQAAKEwc.jpg [url] => https://t.co/P7frCJDZu1 [display_url] => pic.twitter.com/P7frCJDZu1 [expanded_url] => https://twitter.com/ErikGriffin/status/817054214165819392/photo/1 [type] => photo [sizes] => Array ( [medium] => Array ( [w] => 900 [h] => 1200 [resize] => fit ) [small] => Array ( [w] => 510 [h] => 680 [resize] => fit ) [thumb] => Array ( [w] => 150 [h] => 150 [resize] => crop ) [large] => Array ( [w] => 1536 [h] => 2048 [resize] => fit ) ) [source_status_id] => 817054214165819392 [source_status_id_str] => 817054214165819392 [source_user_id] => 16053405 [source_user_id_str] => 16053405 ) ) ) [extended_entities] => Array ( [media] => Array ( [0] => Array ( [id] => 817054192359456768 [id_str] => 817054192359456768 [indices] => Array ( [0] => 114 [1] => 137 ) [media_url] => http://pbs.twimg.com/media/C1bCdYHUQAAKEwc.jpg [media_url_https] => https://pbs.twimg.com/media/C1bCdYHUQAAKEwc.jpg [url] => https://t.co/P7frCJDZu1 [display_url] => pic.twitter.com/P7frCJDZu1 [expanded_url] => https://twitter.com/ErikGriffin/status/817054214165819392/photo/1 [type] => photo [sizes] => Array ( [medium] => Array ( [w] => 900 [h] => 1200 [resize] => fit ) [small] => Array ( [w] => 510 [h] => 680 [resize] => fit ) [thumb] => Array ( [w] => 150 [h] => 150 [resize] => crop ) [large] => Array ( [w] => 1536 [h] => 2048 [resize] => fit ) ) [source_status_id] => 817054214165819392 [source_status_id_str] => 817054214165819392 [source_user_id] => 16053405 [source_user_id_str] => 16053405 ) ) ) [metadata] => Array ( [iso_language_code] => en [result_type] => recent ) [source] => Twitter for Android [in_reply_to_status_id] => [in_reply_to_status_id_str] => [in_reply_to_user_id] => [in_reply_to_user_id_str] => [in_reply_to_screen_name] => [user] => Array ( [id] => 2448946046 [id_str] => 2448946046 [name] => SharlineMarie [screen_name] => SharlineMarle [location] => West Palm [description] => Beneath the Make- Up, and behind the Smile.. Im just a Girl Wishing for the World.. [url] => [entities] => Array ( [description] => Array ( [urls] => Array ( ) ) ) [protected] => [followers_count] => 42 [friends_count] => 1391 [listed_count] => 12 [created_at] => Thu Apr 17 05:02:06 +0000 2014 [favourites_count] => 466 [utc_offset] => -28800 [time_zone] => Pacific Time (US & Canada) [geo_enabled] => [verified] => [statuses_count] => 529 [lang] => en [contributors_enabled] => [is_translator] => [is_translation_enabled] => [profile_background_color] => EDECE9 [profile_background_image_url] => http://abs.twimg.com/images/themes/theme3/bg.gif [profile_background_image_url_https] => https://abs.twimg.com/images/themes/theme3/bg.gif [profile_background_tile] => [profile_image_url] => http://pbs.twimg.com/profile_images/770369635690618882/TvzcYa-Q_normal.jpg [profile_image_url_https] => https://pbs.twimg.com/profile_images/770369635690618882/TvzcYa-Q_normal.jpg [profile_banner_url] => https://pbs.twimg.com/profile_banners/2448946046/1472505412 [profile_link_color] => 088253 [profile_sidebar_border_color] => D3D2CF [profile_sidebar_fill_color] => E3E2DE [profile_text_color] => 634047 [profile_use_background_image] => 1 [has_extended_profile] => [default_profile] => [default_profile_image] => [following] => [follow_request_sent] => [notifications] => [translator_type] => none ) [geo] => [coordinates] => [place] => [contributors] => [retweeted_status] => Array ( [created_at] => Thu Jan 05 17:04:39 +0000 2017 [id] => 817054214165819392 [id_str] => 817054214165819392 [text] => Thank @GAEMS_PGE road life is so much better now! Got my @DestinyTheGame and room service. #nerd https://t.co/P7frCJDZu1 [truncated] => [entities] => Array ( [hashtags] => Array ( [0] => Array ( [text] => nerd [indices] => Array ( [0] => 91 [1] => 96 ) ) ) [symbols] => Array ( ) [user_mentions] => Array ( [0] => Array ( [screen_name] => GAEMS_PGE [name] => GAEMS [id] => 148112305 [id_str] => 148112305 [indices] => Array ( [0] => 6 [1] => 16 ) ) [1] => Array ( [screen_name] => DestinyTheGame [name] => Destiny The Game [id] => 948444535 [id_str] => 948444535 [indices] => Array ( [0] => 57 [1] => 72 ) ) ) [urls] => Array ( ) [media] => Array ( [0] => Array ( [id] => 817054192359456768 [id_str] => 817054192359456768 [indices] => Array ( [0] => 97 [1] => 120 ) [media_url] => http://pbs.twimg.com/media/C1bCdYHUQAAKEwc.jpg [media_url_https] => https://pbs.twimg.com/media/C1bCdYHUQAAKEwc.jpg [url] => https://t.co/P7frCJDZu1 [display_url] => pic.twitter.com/P7frCJDZu1 [expanded_url] => https://twitter.com/ErikGriffin/status/817054214165819392/photo/1 [type] => photo [sizes] => Array ( [medium] => Array ( [w] => 900 [h] => 1200 [resize] => fit ) [small] => Array ( [w] => 510 [h] => 680 [resize] => fit ) [thumb] => Array ( [w] => 150 [h] => 150 [resize] => crop ) [large] => Array ( [w] => 1536 [h] => 2048 [resize] => fit ) ) ) ) ) [extended_entities] => Array ( [media] => Array ( [0] => Array ( [id] => 817054192359456768 [id_str] => 817054192359456768 [indices] => Array ( [0] => 97 [1] => 120 ) [media_url] => http://pbs.twimg.com/media/C1bCdYHUQAAKEwc.jpg [media_url_https] => https://pbs.twimg.com/media/C1bCdYHUQAAKEwc.jpg [url] => https://t.co/P7frCJDZu1 [display_url] => pic.twitter.com/P7frCJDZu1 [expanded_url] => https://twitter.com/ErikGriffin/status/817054214165819392/photo/1 [type] => photo [sizes] => Array ( [medium] => Array ( [w] => 900 [h] => 1200 [resize] => fit ) [small] => Array ( [w] => 510 [h] => 680 [resize] => fit ) [thumb] => Array ( [w] => 150 [h] => 150 [resize] => crop ) [large] => Array ( [w] => 1536 [h] => 2048 [resize] => fit ) ) ) ) ) [metadata] => Array ( [iso_language_code] => en [result_type] => recent ) [source] => Twitter for Android [in_reply_to_status_id] => [in_reply_to_status_id_str] => [in_reply_to_user_id] => [in_reply_to_user_id_str] => [in_reply_to_screen_name] => [user] => Array ( [id] => 16053405 [id_str] => 16053405 [name] => Erik Griffin [screen_name] => ErikGriffin


Вопрос. Как добраться до массива "hashtags" и его ключ/значения text:nerd?
Могу добраться до "statuses" до значения text. Я так понял "statuses" это типа родитель для всех остальных,кто вложен в него. Вот так все вызываю:
$tweetsArray = json_decode($response, true);
foreach($tweetsArray['statuses'] as $item) {
    echo '<li>'.$item['text'].'</li>'."\n";
}


Не знаю как добираться если какой либо массив вложенный. Заранее спасибо
  • Вопрос задан
  • 265 просмотров
Пригласить эксперта
Ответы на вопрос 1
@AlexMt
Automation Q&A Lead
Главным образом добраться можно несколькими квадратными скобками:
$response['statuses'][0]['answers']['post'][1]
или
$response['statuses']['tweets']['answers']['posts']

В первом случае - мы работаем как с индексными так и ассоциативными массивами
а во втором - только с ассоциативными. Для того чтобы работать только с индексами, можно поступать так:
$resposne[1][0][0][5]

Отвечая на твой вопрос, как добраться до hashtags, не мог бы ты привести исходный json в форматированном виде в теге < code > тут?
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
18 апр. 2024, в 21:56
2000 руб./за проект
18 апр. 2024, в 21:00
150 руб./за проект