[英]Display Php (json) output in tabular format
我是Wordpress插件开发的新手,编写了一个函数,该函数请求数据的url并以Json格式获得相同的代码,功能片段如下;
function user_det()
{
$em = '';
$url = ''.$em;
$myhtml = wp_remote_retrieve_body( wp_remote_get($url, array( 'timeout'=>120)));
echo '<pre>' , print_r($myhtml) , '</pre>';
}
输出:
{
"Status":"Success",
"Bookings":[
{
"Message":"",
"Detail":{
"ServiceType":"",
"HoName":"",
"HAddress1":"",
"ToRooms":"",
"RoDetail":[
{
"RoomTypeDescription":[
" "
],
"NumberOfRooms":"",
"Passengers":[
{
"Salutations":"",
"FirstName":"",
"LastName":""
},
{
"Salutations":"",
"Age":"",
"CotBedOption":""
}
],
"totalAdult":2,
"totalChild":0
}
],
"Phone":"-",
"Rating":"",
"email":""
}
}]}
但是,有没有一种方法可以以表格格式显示数据,尝试在线搜索但无法理解
从API请求中获取JSON格式的信息时,您需要迭代每个元素以设置所需的格式,例如使用此信息构建表的示例。
$myhtml = wp_remote_retrieve_body( wp_remote_get($url, array( 'timeout'=>120)));
$dataArray = json_decode($myhtml,1);
echo '<table>';
echo '<thead><tr><th>Service Type</th></tr></thead>';
echo '<tbody>';
// Now we have the json converted into an array, let's iterate it
foreach($dataArray["Bookings"] as $idx=>$bookingData) {
// Iterate all bookings and write on the rows
echo '<tr>';
echo '<td>'.$bookingData['Detail']['ServiceType'].'</td>';
echo '</tr>';
}
echo '</tbody><table>';
我相信这可以使您了解需要执行哪些操作以获取对象并建立表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.