2017-04-09 76 views
0

我使用引导数据表来显示表中的API数据。这里是数据:使用PHP来循环浏览json数组并输出html

{ 
    "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" 
     } 
    ] 
    } 
} 

我使用引导数据表来显示表中的API数据。下面是数据:

{ 
    "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" 
     } 
    ] 
    } 
} 
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> 

到目前为止,我已经与这个

<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> 
    <?php foreach($data->response_status->status_message as $message) : ?> 
    <tr> 
     <td><?php echo $message->reg_number; ?></td> 
     <td><?php echo $message->company; ?></td> 
     <td><?php echo $message->office; ?></td> 
     <td><?php echo $message->phone_number; ?></td> 
     <td><?php echo $message->postal_address; ?></td> 
     <td><?php echo $message->postal_code; ?></td> 
    </tr> 
    <?php endforeach; ?> 
</tbody> 
<table> 

但它不工作玩耍。有没有任何解决方法,使其工作?

+0

你是'json_decode'数据吗?你是否尝试过使用'json_decode'的第二个参数来返回一个关联数组?你得到的错误是什么? – Scopey

回答

0

似乎一切都正常,因为@Scopey评论,唯一缺少的是在这里json_decode

$json = file_get_contents('path/to/your/JSONfile.json'); 
    $data= json_decode($json); 

如果你这样做了,那么没有什么是错的。