我正在研究将JSON和/或XML中的一些数据解析成表。
我正在寻找帮助我解决这个问题的人。我需要将JSON或XML中的多个项解析到表中。下面我给出我的JSON示例:
{"appartments":[{"aptnum":"199","design":"open","sqft":"1200","extras":"covered parking","pool":"yes","moveinDate":"2019-01-01 13:12:01","link":"https:\/\/www.demoapts.com\/demo\/199"},{"aptnum":"223","design":"Built Already","sqft":"1800","extras":"covered parking","pool":"yes","moveinDate":"2018-05-09 00:12:01","link":"https:\/\/www.demoapts.com\/demo\/223"}]
我需要帮助的是将这些数据解析为html / Wordpress表。
我也使用了一种特殊类型的按钮,但我想如果我能学会如何正确解析数据,我就知道了。
我希望你们中的一些人可以帮助我并指出我正确的方向。我在谷歌搜索过,我只找到了从JSON中解析一个项目的例子。
答案 0 :(得分:2)
以下是有关如何将此JSON结构解析为表
的示例<?php
$data = json_decode('{"appartments":[{"aptnum":"199","design":"open","sqft":"1200","extras":"covered parking","pool":"yes","moveinDate":"2019-01-01 13:12:01","link":"https:\/\/www.demoapts.com\/demo\/199"},{"aptnum":"223","design":"Built Already","sqft":"1800","extras":"covered parking","pool":"yes","moveinDate":"2018-05-09 00:12:01","link":"https:\/\/www.demoapts.com\/demo\/223"}]}');
// Convert JSON string to PHP object.
$appartments = $data->appartments;
echo('<table>');
if(!empty($appartments)){
echo('<thead><tr>');
// Using the first object to print column names.
foreach($appartments[0] as $key => $value){
echo('<th>' . $key . '</th>');
}
echo('</tr></thead>');
echo('<tbody>');
// Iterate through all appartments and print them as table cells.
foreach($appartments as $appartment){
echo('<tr>');
foreach($appartment as $key => $value){
echo('<td>' . $value . '</td>');
}
echo('</tr>');
}
echo('</tobdy></table>');
}
?>