从JSON转换为HTML表格,格式如下

时间:2017-09-26 09:22:12

标签: php jquery html json html5

我有一个像JSON这样反序列化的对象:

{
  "Wilayah": "BALI ",
  "Jns_Brg": "WATER DISPANSER ",
  "Kd_Brg": "WDP-300 ",
  "Kd_Trn": "J",
  "Qty": "159",
  "Bulan": 7,
  "Tahun": 2017
}, {
  "Wilayah": "BANDUNG ",
  "Jns_Brg": "WATER DISPANSER",
  "Kd_Brg": "WDP-300 ",
  "Kd_Trn": "J",
  "Qty": "2522",
  "Bulan": 7,
  "Tahun": 2017
}, {
  "Wilayah": "BANDUNG ",
  "Jns_Brg": "BLENDER ",
  "Kd_Brg": "BL-101 ",
  "Kd_Trn": "J",
  "Qty": "4554",
  "Bulan": 7,
  "Tahun": 2017
}

我想像这样在表格中显示数据。

Kd_Brg | Bali | Bandung
       | 7    |  7
========================
WDP-300| 159  | 2522
BL-101 |      | 4554

我该如何处理?后端php,前端bootstrap jquery + html

1 个答案:

答案 0 :(得分:1)

<?php
    $json= "your json code";
    $data =  json_decode($json);
    if (count($data->stand)){
        // Open the table
        echo "<table>";

        // Cycle through the array
        foreach ($data as $key => $value) {

            // Output a row
            echo "<tr>";
            echo "<td>".$key."</td>";
            echo "<td>".$value."</td>";
            echo "</tr>";
        }

        // Close the table
        echo "</table>";
    }
?>