$events = array();
$employeeData = array();
foreach ($rows as $fetch)
{
$employeeData['start_date'] = $fetch->start_date;
$employeeData['events']['start_time'] = $fetch->start_time;
$employeeData['events']['end_time'] = $fetch->end_time;
$employeeData['events']['name'] = $fetch->FirstName. ' ' .$fetch->LastName;
array_push($events, $employeeData);
}
$success_status = array(
'events'=> $events
);
echo json_encode($success_status, true);
结果------------
{“事件”:[{“开始日期”:“ 2018-05-27”,“事件”:{ “开始时间”:“ 11:45:00”,“结束时间”:“ 13:00:00”,“名称”: “ Demo”}},{“ start_date”:“ 2018-06-29”,“ events”:{ “开始时间”:“ 15:30:00”,“结束时间”:“ 15:45:00”,“名称”: “ Demo”}},{“ start_date”:“ 2018-06-29”,“ events”:{ “开始时间”:“ 16:30:00”,“结束时间”:“ 23:45:00”,“名称”: “演示”}}]}
我得到上述结果,但我需要得到以下结果
{“事件”:[{“开始日期”:“ 2018-05-27”,“事件”:[{ “开始时间”:“ 11:45:00”,“结束时间”:“ 13:00:00”,“名称”: “ Demo”}]},{“ start_date”:“ 2018-06-29”,“ events”:[{ “开始时间”:“ 15:30:00”,“结束时间”:“ 15:45:00”,“名称”: “ Demo”},{“ start_time”:“ 16:30:00”,“ end_time”:“ 23:45:00”, “ name”:“ Demo”}]}]}
答案 0 :(得分:1)
我认为这应该在之后产生输出,这是在要嵌套在[{...}]
中的数据上添加额外的数组层的情况。
$employeeData['events'][] = ['start_time' => $fetch->start_time,
'end_time' =>$fetch->end_time,
'name' => $fetch->FirstName. ' ' .$fetch->LastName ];
因此,它使用[]
仅将数据添加到一个数组中,而我将元素包装到一个数组中,而不是将其添加为项。
答案 1 :(得分:0)
$employeeData['events']['start_time']
基本上是将单个项目创建为关联数组。您想要的是一系列事件项。因此,您需要将事件数据放入一个数组中,然后将该数组添加到events
数组中,如下所示:
$events = array();
$employeeData = array();
foreach ($rows as $fetch)
{
$event = [];
$employeeData['start_date'] = $fetch->start_date;
$event['start_time'] = $fetch->start_time;
$event['end_time'] = $fetch->end_time;
$event['name'] = $fetch->FirstName. ' ' .$fetch->LastName;
$employeeData['events'][] = $event;
array_push($events, $employeeData);
}
$success_status = array(
'events'=> $events
);
echo json_encode($success_status, true);