所以我有一个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>';
}
收到以下错误:
警告:为foreach()提供的参数无效 在第28行
这是我获取数据的脚本:
<?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>";
?>
所以我的问题是:我如何发布当前价格?
答案 0 :(得分:1)
这更好地写为
echo '<td>'. htmlspecialchars($val['current']['price']) .'</td></tr>';
你试图迭代$arr
,这是一个不包含名为current
的元素的外部数组
答案 1 :(得分:1)
$json_str=' {
"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
}
}
]
}';
$arr = json_decode($json_str,true);
// this $arr contains array with-in array so there is no direct access to
// 'Price' its either inside current or today so access them accordingly like
$arr['current']['price'];
//or
$arr['today']['price'];
答案 2 :(得分:0)
如果current中的属性并不总是返回相同的键,则可以使用:
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 border='1'>";
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($val['current'] as $v){
echo '<td>'. htmlspecialchars($v) .'</td>';
}
}
echo "</table>";
答案 3 :(得分:0)
试试这个。
首先检查数组是否为空。如果数组为空,则会因为传递关联数组而导致错误,并且在第一个循环中没有关闭tr标记。
if(!empty($arr))
{
//for loop
}
else
{
echo "Array empty";
}