繁体   English   中英

将json数据放入表中

[英]Putting json data into table

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

?>

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

这写得更好

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

你试图迭代$arr ,这是一个不包含一个名为current的元素的outter数组

$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'];

如果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>";

试试这个吧。

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

if(!empty($arr))
{
     //for loop
}
else
{
    echo "Array empty";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM