我非常初学者,所以请多多包涵。 我正在尝试在表中显示JSON响应。 我已经很好地检索了JSON数据,但是要遍历以获取要显示的数据,结构有点复杂。
这是学校的时间表,所以我要使用的格式如下:
Session | Monday | Tuesday | Wednesday
-----------------------------------------------------------
Home Room | 2HRS - JS1.6 | 2HRS - JS1.6 | ....
08:30 - 08:45 | Homeroom | Homeroom | ....
| Ms Jones | Ms Jones | ....
| | |
Period 1 | 2ENS - JS1.6 | 2ENS - JS1.6 | ....
08:45 - 09:35 | English | English | ....
| Ms Jones | Ms Jones | ....
我想做的第一件事是遍历房间并将它们显示在桌子上。
这是我的JSON响应的开始:
{
"data": [
{
"description": "Home Room",
"from_time": "08:30",
"to_time": "08:45",
"timetable": {
"1": {
"DayNumber": "1",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
},
"2": {
"DayNumber": "2",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
},
"3": {
"DayNumber": "3",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
},
"4": {
"DayNumber": "4",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
},
"5": {
"DayNumber": "5",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
}
}
},
{
"description": "Period 1",
"from_time": "08:45",
"to_time": "09:35",
"timetable": {
"1": {
"DayNumber": "1",
"ClassCode": "2ENS",
"ClassDescription": "English",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:45",
"EndTime": "09:35"
},
"2": {
"DayNumber": "2",
"ClassCode": "2ENS",
"ClassDescription": "English",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:45",
"EndTime": "09:35"
},
"3": {
"DayNumber": "3",
"ClassCode": "2ENS",
"ClassDescription": "English",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:45",
"EndTime": "09:35"
},
"4": {
"DayNumber": "4",
"ClassCode": "2ENS",
"ClassDescription": "English",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:45",
"EndTime": "09:35"
},
"5": {
"DayNumber": "5",
"ClassCode": "2ENS",
"ClassDescription": "English",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:45",
"EndTime": "09:35"
}
}
}
]}
这是我的一些代码。
$response = json_decode($get_data, true);
$rooms = $response['data']['0']['timetable'];
?>
<?php foreach ($rooms['1'] as $v) { ?>
<tr>
<td></td>
<td class="first"><?php echo $v;} ?></td>
</tr>
这将循环遍历“ 1”数组,并在每行上显示以下数据。 我似乎无法沿着结构向下导航足够远的距离以进入房间。
1
2HRS
Homeroom
JS.1.6
Yasemin Sali
Ms
Sali
08:30
08:45
我在这里可能完全偏离了轨道,因此任何指导将不胜感激。
*****************编辑********************************** *****
Session | Monday | Tuesday |
--------------------------------------------------------------------
"description" | "ClassCode" - "Room" | .... |
"from_time" - "to_time" or | "ClassDescription" | .... |
"StartTime" - "EndTime" | "StaffTitle" "StaffSurname" | .... |
---------------------------|-----------------------------|---------|
"description" | "ClassCode" - "Room" | .... |
"from_time" - "to_time" | "ClassDescription" | .... |
"StartTime" - "EndTime" | "StaffTitle" "StaffSurname" | .... |
| (if timetable #A exists then| |
| add below) | |
| ------------------- | |
| "ClassCode" - "Room" | |
| "ClassDescription" | |
| "StaffTitle" "StaffSurname" | |
---------------------------|-----------------------------|---------|
*******************编辑2 ************************** *********
{
"description": "Recess",
"from_time": "10:25",
"to_time": "10:45",
"timetable": {
"1": [],
"2": [],
"3": [],
"4": [],
"5": []
}
},
******************编辑3 *************************** ***************
{
"description": "Period 3",
"from_time": "10:45",
"to_time": "11:35",
"timetable": {
"1": {
"DayNumber": "1",
"ClassCode": "2ENS",
"ClassDescription": "English",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "10:45",
"EndTime": "11:35"
},
"2": {
"DayNumber": "2",
"ClassCode": "2ARS",
"ClassDescription": "Art",
"Room": "AR.1.10",
"StaffNameExternal": "Betty Smith",
"StaffTitle": "Ms",
"StaffSurname": "Smith",
"StartTime": "10:45",
"EndTime": "11:35"
},
"3": {
"DayNumber": "3",
"ClassCode": "2PES",
"ClassDescription": "Physical Education",
"Room": "SP.1.1",
"StaffNameExternal": "Anne Doe",
"StaffTitle": "Mrs",
"StaffSurname": "Doe",
"StartTime": "10:45",
"EndTime": "11:35"
},
"4": {
"DayNumber": "4",
"ClassCode": "2MAS",
"ClassDescription": "Mathematics",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "10:45",
"EndTime": "11:35"
},
"5": {
"DayNumber": "5",
"ClassCode": "2LBS",
"ClassDescription": "Library",
"Room": "",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "10:45",
"EndTime": "11:35"
},
"5A": [
{
"DayNumber": "5",
"ClassCode": "2LBS2",
"ClassDescription": "Library",
"Room": "RC.1.2",
"StaffNameExternal": "Rebecca Williams",
"StaffTitle": "Mrs",
"StaffSurname": "Gallagher",
"StartTime": "10:45",
"EndTime": "11:35"
}
]
}
},
答案 0 :(得分:2)
$response = json_decode($get_data, true);
<table>
<thead>
<tr>
<th>Session</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
</tr>
</thead>
<tbody>
<?php foreach ($response['data'] as $data): ?>
<tr>
<td>
<?php echo $data['description']; ?><br/>
<?php echo $data['from_time'] . ' - ' . $data['to_time']; ?>
</td>
<?php foreach ($data['timetable'] as $desc): ?>
<?php if ($data['description'] != "Recess"): ?>
<td>
<?php echo $desc['ClassCode'] . ' - ' . $desc['Room']; ?><br/>
<?php echo $desc['ClassDescription']; ?><br/>
<?php echo $desc['StaffTitle'] . ' - ' . $desc['StaffSurname']; ?>
</td>
<?php else: ?>
<td></td>
<?php endif; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
此代码假设json值具有从星期一到星期五的完整时间表,并且当天的排序是递增的。
答案 1 :(得分:0)
首先请确保您也对JSON进行了验证和简化。
{
"data": [{
"description": "Home Room",
"from_time": "08:30",
"to_time": "08:45",
"timetable": [
{
"DayNumber": "1",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
},
{
"DayNumber": "2",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
},
{
"DayNumber": "3",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
},
{
"DayNumber": "4",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
},
{
"DayNumber": "5",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "09:35"
}
]
}]
}
然后您可以执行类似的操作
<?php
$get_data = file_get_contents('inf.json');
$response = json_decode($get_data, true);
$rooms = $response['data']['0']['timetable'];
?>
<?php foreach ($rooms as $v) {
echo $v['DayNumber'];
echo " ";
echo $v['ClassCode'];
echo " ";
echo $v['ClassDescription'];
echo $v['Room'];
echo " ";
echo $v['StaffNameExternal'];
echo " ";
echo $v['StaffTitle'];
echo "<br>";
}
?>