使用PHP将JSON输出到HTML表 [英] Outputing JSON to an HTML table using PHP

查看:131
本文介绍了使用PHP将JSON输出到HTML表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用引导程序数据表在表中显示来自API的数据.这是数据:

I using bootstrap datatables to display data from an API in a table. Here is the data:

{
  "response_status": {
    "status_code": 0,
    "status_message": [
      {
        "reg_number": "123",
        "company": "Company A",
        "office": "Orlando",
        "phone_number": "28122312345",
        "postal_address": "000",
        "postal_code": "000"
      },
      {
        "reg_number": "552",
        "company": "Company B",
        "office": "Miami",
        "phone_number": "28122312345",
        "postal_address": "000",
        "postal_code": "000"
      },
      {
       "reg_number": "154",
        "company": "Company C",
        "office": "San Francisco",
        "phone_number": "28122312345",
        "postal_address": "000",
        "postal_code": "000"
      }
    ]
  }
}

我需要结果以这样的方式输出html:

I need the result to output html as this:

<table>
<thead>
   <tr>
       <th>Reg No</th>
       <th>Company</th>
       <th>Office</th>
       <th>Phone Number</th>
       <th>Postal Address</th>
       <th>Postal Code</th>
   </tr>
</thead>

<tbody>
    <tr>
       <td>123</td>
       <td>Company A</td>
       <td>San Francisco</td>
       <td>28122312345</td>
       <td>000</td>
       <td>000</td>
   </tr>
   <tr>
       <td>552</td>
       <td>Company B</td>
       <td>Miami</td>
       <td>28122312345</td>
       <td>000</td>
       <td>000</td>
   </tr>
   <tr>
       <td>154</td>
       <td>Company C</td>
       <td>San Francisco</td>
       <td>28122312345</td>
       <td>000</td>
       <td>000</td>
   </tr>
</tbody>
<table>

有没有一种方法可以遍历JSON正文,并使用php foreach循环自动将"status_message": [];之间所有字段的循环输出到我的表?

Is there a way I can iterate through the JSON body and and automatically output the loop for all the fields between "status_message": []; using a php foreach loop to my table?

推荐答案

考虑到您的数据存储在某个变量中,例如$ your_data ... 使用json_decode($ your_data,true)以数组格式获取数据...然后循环数组

Considering your data is stored in some variable say $your_data... Use json_decode($your_data,true) to get the data in array format... then loop the array

这篇关于使用PHP将JSON输出到HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆