0

所以我有一个 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>";

?>

所以我的问题是:我如何发布当前价格?

4

4 回答 4

1

这最好写成

 echo '<td>'. htmlspecialchars($val['current']['price']) .'</td></tr>';     

你试图迭代$arr哪个是不包含名为的元素的外部数组current

于 2013-08-06T07:46:45.770 回答
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'];
于 2013-08-06T07:58:36.197 回答
0

试试这个。

首先检查数组是否为空。如果数组为空,则会出错,因为您正在传递一个关联数组,并且在第一个循环中没有关闭 tr 标签。

if(!empty($arr))
{
     //for loop
}
else
{
    echo "Array empty";
}
于 2013-08-06T08:00:55.823 回答
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>";
于 2013-08-06T07:52:26.870 回答