所以我有一个 json 文件将它输入到一个表中。现在我想将价格发布到,但我不知道如何
这是我的json:
{
"total": 1,
"items": [
{
"icon": "http://services.runescape.com/m=itemdb_rs/4173_obj_sprite.gif?id=11230",
"icon_large": "http://services.runescape.com/m=itemdb_rs/4173_obj_big.gif?id=11230",
"id": 11230,
"type": "Ammo",
"typeIcon": "http://www.runescape.com/img/categories/Ammo",
"name": "Dragon dart",
"description": "A deadly throwing dart with a dragon tip.",
"current": {
"trend": "neutral",
"price": 184
},
"today": {
"trend": "neutral",
"price": 0
}
}
]
}
现在我可以发布项目图标 ID 类型和名称。但我也希望发布当前价格。我已经尝试过以下方法:
foreach($arr['current'] as $val2){
echo '<td>'. htmlspecialchars($val2['price']) .'</td></tr>';
}
收到以下错误:
警告:在第 28 行为 foreach() 提供的参数无效
这是我获取数据的脚本:
<?php
//check if you have curl loaded
if(!function_exists("curl_init")) die("cURL extension is not installed");
$url = 'http://services.runescape.com/m=itemdb_rs/api/catalogue/items.json?category=1&alpha=d&page=1';
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$r=curl_exec($ch);
curl_close($ch);
$arr = json_decode($r,true);
echo "<table>";
foreach($arr['items'] as $val)
{
echo '<tr>';
echo '<td>'.$val['id'].'</td>';
echo '<td>'. htmlspecialchars($val['type']) .'</td>';
echo '<td>'. htmlspecialchars($val['name']) .'</td>';
echo '<td><img src='. $val['icon'].'></td>';
}
foreach($arr['current'] as $val2){
echo '<td>'. htmlspecialchars($val2['price']) .'</td></tr>';
}
echo "</table>";
?>
所以我的问题是:我如何发布当前价格?