質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

1回答

654閲覧

PHPでjsonの重複するデータを一つにしたい。

退会済みユーザー

退会済みユーザー

総合スコア0

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2019/07/04 07:04

編集2019/07/04 07:49

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?>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yambejp

2019/07/04 07:06

例示のデータがJSONとしては不正確なものなので ただしいデータを再提示してください
退会済みユーザー

退会済みユーザー

2019/07/04 07:11

jsonからphpの配列に変換してarray_*関数で消化できそうな気がするけど、すぐテストできそうなjsonが提示されるまで待機。
m.ts10806

2019/07/04 09:33

初心者アイコンをつけてください
guest

回答1

0

ベストアンサー

php

1<?php 2$json_str = <<<EoL 3{ 4 "statuses": [ 5 { 6 "created_at": "Thu Jul 04 05:19:33 +0000 2019", 7 "id": "1146649726030249984", 8 "id_str": "1146649726030249984", 9 "text": "", 10 "truncated": false, 11 "entities": { 12 "hashtags": [ 13 { 14 "text": "テスト", 15 "indices": [ 16 0, 17 7 18 ] 19 } 20 ], 21 "symbols": [], 22 "user_mentions": [], 23 "urls": [] 24 }, 25 "metadata": { 26 "iso_language_code": "ja", 27 "result_type": "recent" 28 }, 29 "source": "", 30 "in_reply_to_status_id": null, 31 "in_reply_to_status_id_str": null, 32 "in_reply_to_user_id": null, 33 "in_reply_to_user_id_str": null, 34 "in_reply_to_screen_name": null, 35 "user": { 36 "id": "1075985828399046656", 37 "id_str": "1075985828399046656", 38 "name": "略", 39 "screen_name": "略", 40 "location": "", 41 "description": "略", 42 "url": null, 43 "entities": { 44 "description": { 45 "urls": [] 46 } 47 } 48 } 49 }, 50 { 51 "created_at": "Thu Jul 04 05:19:33 +0000 2019", 52 "id": "1146649726030249984", 53 "id_str": "1146649726030249984", 54 "text": "", 55 "truncated": false, 56 "entities": { 57 "hashtags": [ 58 { 59 "text": "テスト", 60 "indices": [ 61 0, 62 7 63 ] 64 } 65 ], 66 "symbols": [], 67 "user_mentions": [], 68 "urls": [] 69 }, 70 "metadata": { 71 "iso_language_code": "ja", 72 "result_type": "recent" 73 }, 74 "source": "", 75 "in_reply_to_status_id": null, 76 "in_reply_to_status_id_str": null, 77 "in_reply_to_user_id": null, 78 "in_reply_to_user_id_str": null, 79 "in_reply_to_screen_name": null, 80 "user": { 81 "id": "1075985828399046656", 82 "id_str": "1075985828399046656", 83 "name": "略", 84 "screen_name": "略", 85 "location": "", 86 "description": "略", 87 "url": null, 88 "entities": { 89 "description": { 90 "urls": [] 91 } 92 } 93 } 94 } 95 ] 96} 97EoL; 98$assoc = json_decode($json_str,true); 99var_export(array_unique($assoc['statuses'], SORT_REGULAR));

投稿2019/07/04 12:01

papinianus

総合スコア12705

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2019/07/04 12:04

ありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問