PHP初心者です。
PHPでtwitterのデータをjsonでまとめています。apiでデータを取得、jsonにFILE_APPENDで追記する際に同じデータがあった場合一つにしたいです。
前提・実現したいこと
↓文字制限のため、一部割愛
JSON
1 { 2 "statuses": [ 3 { 4 "created_at": "Thu Jul 04 05:19:33 +0000 2019", 5 "id": "1146649726030249984", 6 "id_str": "1146649726030249984", 7 "text": "#テスト\n狭山市\nイチゴ農家があります", 8 "truncated": false, 9 "entities": { 10 "hashtags": [ 11 { 12 "text": "テスト", 13 "indices": [ 14 0, 15 7 16 ] 17 } 18 ], 19 "symbols": [], 20 "user_mentions": [], 21 "urls": [] 22 }, 23 "metadata": { 24 "iso_language_code": "ja", 25 "result_type": "recent" 26 }, 27 "source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Twitter Web App</a>", 28 "in_reply_to_status_id": null, 29 "in_reply_to_status_id_str": null, 30 "in_reply_to_user_id": null, 31 "in_reply_to_user_id_str": null, 32 "in_reply_to_screen_name": null, 33 "user": { 34 "id": "1075985828399046656", 35 "id_str": "1075985828399046656", 36 "name": "略, 37 "screen_name": "略", 38 "location": "", 39 "description": "略", 40 "url": null, 41 "entities": { 42 "description": { 43 "urls": [] 44 } 45 }{ 46 "statuses": [ 47 { 48 "created_at": "Thu Jul 04 05:19:33 +0000 2019", 49 "id": "1146649726030249984", 50 "id_str": "1146649726030249984", 51 "text": "#テスト\n狭山市\nイチゴ農家があります", 52 "truncated": false, 53 "entities": { 54 "hashtags": [ 55 { 56 "text": "テスト", 57 "indices": [ 58 0, 59 7 60 ] 61 } 62 ], 63 "symbols": [], 64 "user_mentions": [], 65 "urls": [] 66 }, 67 "metadata": { 68 "iso_language_code": "ja", 69 "result_type": "recent" 70 }, 71 "source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Twitter Web App</a>", 72 "in_reply_to_status_id": null, 73 "in_reply_to_status_id_str": null, 74 "in_reply_to_user_id": null, 75 "in_reply_to_user_id_str": null, 76 "in_reply_to_screen_name": null, 77 "user": { 78 "id": "1075985828399046656", 79 "id_str": "1075985828399046656", 80 "name": "略", 81 "screen_name": "略", 82 "location": "", 83 "description": "略", 84 "url": null, 85 "entities": { 86 "description": { 87 "urls": [] 88 } 89 }
上記のように重複しているデータあり、
↓正しいデータ
JSON
1 2{ 3 "statuses": [ 4 { 5 "created_at": "Thu Jul 04 05:19:33 +0000 2019", 6 "id": "1146649726030249984", 7 "id_str": "1146649726030249984", 8 "text": "#テスト\n狭山市\nイチゴ農家があります", 9 "truncated": false, 10 "entities": { 11 "hashtags": [ 12 { 13 "text": "テスト", 14 "indices": [ 15 0, 16 7 17 ] 18 } 19 ], 20 "symbols": [], 21 "user_mentions": [], 22 "urls": [] 23 }, 24 "metadata": { 25 "iso_language_code": "ja", 26 "result_type": "recent" 27 }, 28 "source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Twitter Web App</a>", 29 "in_reply_to_status_id": null, 30 "in_reply_to_status_id_str": null, 31 "in_reply_to_user_id": null, 32 "in_reply_to_user_id_str": null, 33 "in_reply_to_screen_name": null, 34 "user": { 35 "id": "1075985828399046656", 36 "id_str": "1075985828399046656", 37 "name": "略", 38 "screen_name": "略", 39 "location": "", 40 "description": "略", 41 "url": null, 42 "entities": { 43 "description": { 44 "urls": [] 45 } 46 }, 47 "protected": false, 48 "followers_count": 1, 49 "friends_count": 3, 50 "listed_count": 0, 51 "created_at": "Fri Dec 21 05:26:27 +0000 2018", 52 "favourites_count": 1, 53 "utc_offset": null, 54 "time_zone": null, 55 "geo_enabled": false, 56 "verified": false, 57 "statuses_count": 49, 58 "lang": null, 59 "contributors_enabled": false, 60 "is_translator": false, 61 "is_translation_enabled": false, 62 "profile_background_color": "F5F8FA", 63 "profile_background_image_url": null, 64 "profile_background_image_url_https": null, 65 "profile_background_tile": false, 66 "profile_image_url": "http://pbs.twimg.com/profile_images/1075989853701984256/cfTYj2mr_normal.jpg", 67 "profile_image_url_https": "https://pbs.twimg.com/profile_images/1075989853701984256/cfTYj2mr_normal.jpg", 68 "profile_link_color": "1DA1F2", 69 "profile_sidebar_border_color": "C0DEED", 70 "profile_sidebar_fill_color": "DDEEF6", 71 "profile_text_color": "333333", 72 "profile_use_background_image": true, 73 "has_extended_profile": true, 74 "default_profile": true, 75 "default_profile_image": false, 76 "following": false, 77 "follow_request_sent": false, 78 "notifications": false, 79 "translator_type": "none" 80 }, 81
上のように一つにまとめたい。
該当のソースコード
PHP
1<?php 2 3//文字制限でAPIは割愛 4$json = json_encode($tweet, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); 5 file_put_contents('./tweets.json', $json, FILE_APPEND); 6?>
回答1件
あなたの回答
tips
プレビュー