すでに取得したjson形式のデータをphpを利用して表示させたい
ここに質問の内容を詳しく書いてください。
phpを使って取得したjson形式のデータを表示させようとしています。
しかし、jsonにまだ不慣れで値へのアクセスの仕方がわかりません。
現在扱っているjsonの形と試したコードを貼り付けます。
試したコード
php
1<!DOCTYPE html> 2<html lang="ja" dir="ltr"> 3<head> 4 <meta charset="utf-8"> 5 <title>test</title> 6</head> 7<body> 8<?php 9/** 10 * Created by PhpStorm. 11 * User: 12 * Date: 13 * Time: 14 */ 15$url = "./test.json"; 16$json = file_get_contents($url); 17$json = mb_convert_encoding($json, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN'); 18$arr = json_decode($json, true); 19 20print("<table border=1>"); 21 22foreach ($arr as $elm) { 23 print("<tr>"); 24 print("<td>" . $elm['trends']['name'] . "</td>"); 25 print("<td>" . $elm['trends']['url'] . "</td>"); 26 print("</tr>"); 27} 28print("</table>"); 29?> 30</body> 31</html>
###扱うデータ
[ {"trends": [ {"name":"\u5EA6","url":"http://search?","promoted_content":null,"query":"%E5%BA%A6","volume":10583 }, {"name":"aaa","url":"http://bbb","promoted_content":null,"query":"ccc","volume":12345 }, ... {"name":"AAA","url":"http://BBB","promoted_content":null,"query":"CCC","volume":67890 } ], "as_of":"2018-07-06T08:17:55Z","created_at":"2018-07-06T08:09:27Z","locations": [ {"name":"Kitakyushu","woeid":1110809 } ] } ]
試したこと
このまま実行して
Notice: Undefined index: name in I:\php_test\test.php on line 24
Notice: Undefined index: url in I:\php_test\test.php on line 25
と言われました。
補足情報
jsonデータは適当に短くしています。“name”や“url”をforeachで表示させる方法を知りたいです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/08 07:40