我需要这种 JSON 格式:
{
"1" : [{"start": "", "end": ""}],
"2" : [{"start": "", "end": ""}],
"3" : [{"start": "", "end": ""}],
"4" : [{"start": "", "end": ""}],
"5" : [{"start": "", "end": ""}],
"6" : [{"start": "", "end": ""}],
"7" : [{"start": "", "end": ""}]
}
但是使用 PHP 我得到了这种格式:
[
{"1" : [{"start": "", "end": ""}]},
{"2" : [{"start": "", "end": ""}]},
{"3" : [{"start": "", "end": ""}]},
{"4" : [{"start": "", "end": ""}]},
{"5" : [{"start": "", "end": ""}]},
{"6" : [{"start": "", "end": ""}]},
{"7" : [{"start": "", "end": ""}]},
]
索引“1”、“2”……对应于一周中的一天。每个索引都有 'n' 个小时范围(数组),格式为 {start: "", end: ""},...
例如:{start: "12:00:00",end:"14:00:00"},{start: "14:00:00","15:00:00"},...
我使用 PHP 函数从表中获取天数,然后使用“foreach”进行迭代以在每次迭代中获取同一表中的小时范围,然后迭代这些范围以创建所需的格式。
最后一步是使用“json_encode”进行格式化。
//Getting all days data from DB given user ID
$aDays = Schedule::getDays($nIdUser);
//Array to Store Data
$aJson = [];
//Iteration
foreach ($aDays as $aInfoDay) {
//Getting single day hour range given day ID
$aRangesHours = Schedule::getDayRanges("id_day = {$aInfoDay['id_day']} AND id_user = {$nIdUser}");
$aParsedDay = [];
//Iterate Ranges
foreach ($aRangesHours as $aRangeHour) {
$aParsedDay[$aRangeHour['id_day']][] = array('start' => $aRangeHour['startHour'],'end' => $aRangeHour['endHour']);
}
//Store Data in aJson Array
$aJson[] = $aParsedDay;
}
侃侃无极
婷婷同学_
慕森卡