1 回答
TA贡献2051条经验 获得超10个赞
您可以使用mapToGroups获取请求的格式。但是我不知道你的表结构所以我得到了这些;
[{"Monday":"11:30"},{"Monday":"12:00"},{"Monday":"23:00"},{"Tuesday":"11:30"},{"Tuesday":"12:00"},{"Tuesday":"23:00"}]
放入集合中(假设你雄辩的集合像那样返回)
$timings = collect([
["Monday" => "11:30"],
["Monday" => "12:00"],
["Monday" => "23:00"],
["Tuesday" => "11:30"],
["Tuesday" => "12:00"],
["Tuesday" => "23:00"]
]);
return $timings->mapToGroups(function ($item) {
$day = array_key_first($item);
return [$day => $item[$day]];
});
打印这些;
{
"Monday": [
"11:30",
"12:00",
"23:00"
],
"Tuesday": [
"11:30",
"12:00",
"23:00"
]
}
编辑:(你在网上发布的相同数组在这里使用+我用于解决方案的相同功能)
$days = ["Monday", "Monday"];
$time = ["11:30", "12:30"];
$doctor_timings = [];
for ($i = 0; $i < sizeof($days); $i++) {
$day_id = $days[$i];
$time_id = $time[$i];
array_push($doctor_timings, [$day_id => $time_id]);
}
return collect($doctor_timings)->mapToGroups(function ($item) {
$day = array_key_first($item);
return [$day => $item[$day]];
});
哪个打印
{"Monday":["11:30","12:30"]}
- 1 回答
- 0 关注
- 94 浏览
添加回答
举报