dpfw3607 2016-02-01 15:52
浏览 136
已采纳

如何使用PHP将json解析为html表?

I have json file like this

[{
    "Shop_name": "916",
    "Shop_id": "916TCR",
    "Address":"cdsasffafa"
    "numbers": "4",
    "mob_no": "9447722856"
}, {
    "Shop_name": "Chicking",
    "Shop_id": "CKGTCR",
    "Address":"afagagg",
    "numbers": "8",
    "mob_no": "6767564532"
}]

i want to see this as HTML Table with row head as Shop_name,Shop_id,Address and Data in next row.

i tried decoding it and display as table (How can i parse json into a html table using PHP?). but Json File format is diffrent here. Also like to know if Json File gets bigger with [0],[1],[2]....

  • 写回答

1条回答 默认 最新

  • doubingqi5829 2016-02-01 16:04
    关注

    You should parse your json data to php object than you should iterate your data like below;

    $myData = <<<JSON
    
    [
    {"Shop_name":"minh",
      "Shop_id":"916TCR",
      "Address":"cdsasffafa",
      "numbers":"4",
      "mob_no":"9447722856"
    },
    {"Shop_name":"Chig",
     "Shop_id":"CKGTCR",
     "Address":"afagagg",
     "numbers":"8",
     "mob_no":"6767564532"
    }
    ]
    
    JSON;
    
    $myObject = json_decode($myData);
    
    ?>
    <table>
    <tr>
        <td>Shop Name</td>
        <td>Shop ID</td>
        <td>Address</td>
        <td>Numbers</td>
        <td>Mob No</td>
    </tr>
    <?PHP
    foreach($myObject as $key=>$item)
    {
        ?>
        <tr>
            <td><?PHP echo $item->Shop_name; ?></td>
            <td><?PHP echo $item->Shop_id; ?></td>
            <td><?PHP echo $item->Address; ?></td>
            <td><?PHP echo $item->numbers; ?></td>
            <td><?PHP echo $item->mob_no; ?></td>
        </tr>
        <?PHP
    }
    ?>
    </table>
    

    A working example is here: http://ideone.com/IqZLMs

    PS: You should fix this ” quote to "

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂