分组对象数组
Grouping Object Array
你好我有以下JSON:
[
{
"id": 1,
"company_id": 2,
"route_id": 16,
"class": "Standard",
"fare": 35,
"weekday": "monday",
"reporting_time": "06:00 PM",
"departure_time": "07:00 PM",
"extras": []
},
{
"id": 2,
"company_id": 2,
"route_id": 16,
"class": "Standard",
"fare": 35,
"weekday": "tuesday",
"reporting_time": "06:00 PM",
"departure_time": "07:00 PM",
"extras": []
},
{
"id": 3,
"company_id": 2,
"route_id": 16,
"class": "Standard",
"fare": 35,
"weekday": "wednesday",
"reporting_time": "06:00 PM",
"departure_time": "07:00 PM",
"extras": []
},
{
"id": 4,
"company_id": 2,
"route_id": 16,
"class": "VIP",
"fare": 35,
"weekday": "thursday",
"reporting_time": "06:00 PM",
"departure_time": "07:00 PM",
"extras": []
},
{
"id": 5,
"company_id": 2,
"route_id": 16,
"class": "VIP",
"fare": 35,
"weekday": "friday",
"reporting_time": "06:00 PM",
"departure_time": "07:00 PM",
"extras": []
},
{
"id": 6,
"company_id": 2,
"route_id": 16,
"class": "Business",
"fare": 35,
"weekday": "saturday",
"reporting_time": "06:00 PM",
"departure_time": "07:00 PM",
"extras": []
}
]
这就是我的 eloquent 模型返回数据的方式,我使用转换器格式化输出以隐藏某些字段等。
我想按 class
属性 对这些数据进行分组。这就是我希望数据的样子:
[
{
"class":"Standard",
"schedules":[
{
"id":1,
"company_id":2,
"route_id":16,
"fare":35,
"weekday":"monday",
"reporting_time":"06:00 PM",
"departure_time":"07:00 PM",
"extras":[
]
},
{
"id":2,
"company_id":2,
"route_id":16,
"fare":35,
"weekday":"tuesday",
"reporting_time":"06:00 PM",
"departure_time":"07:00 PM",
"extras":[
]
},
{
"id":3,
"company_id":2,
"route_id":16,
"fare":35,
"weekday":"wednesday",
"reporting_time":"06:00 PM",
"departure_time":"07:00 PM",
"extras":[
]
}
]
},
{
"class":"VIP",
"schedules":[
{
"id":4,
"company_id":2,
"route_id":16,
"fare":35,
"weekday":"thursday",
"reporting_time":"06:00 PM",
"departure_time":"07:00 PM",
"extras":[
]
},
{
"id":5,
"company_id":2,
"route_id":16,
"fare":35,
"weekday":"friday",
"reporting_time":"06:00 PM",
"departure_time":"07:00 PM",
"extras":[
]
}
]
},
{
"class":"Business",
"schedules":[
{
"id":6,
"company_id":2,
"route_id":16,
"fare":35,
"weekday":"saturday",
"reporting_time":"06:00 PM",
"departure_time":"07:00 PM",
"extras":[
]
}
]
}
]
如何在不改变 eloquent 查询或数据库结构的情况下执行此操作?有没有办法可以使用变压器来实现这一目标?如果没有,我唯一的选择是更改 eloquent 查询,我将如何做才能使数据看起来像我想要的那样?
$result=[];
foreach($youarray as $key => $item)
{
$result[ $item['class'] ]['class'] = $item['class'];
$result[ $item['class'] ]['schedules'] = [$item['id'], $item['company_id'], etc. ];
}
return $result;
你好我有以下JSON:
[
{
"id": 1,
"company_id": 2,
"route_id": 16,
"class": "Standard",
"fare": 35,
"weekday": "monday",
"reporting_time": "06:00 PM",
"departure_time": "07:00 PM",
"extras": []
},
{
"id": 2,
"company_id": 2,
"route_id": 16,
"class": "Standard",
"fare": 35,
"weekday": "tuesday",
"reporting_time": "06:00 PM",
"departure_time": "07:00 PM",
"extras": []
},
{
"id": 3,
"company_id": 2,
"route_id": 16,
"class": "Standard",
"fare": 35,
"weekday": "wednesday",
"reporting_time": "06:00 PM",
"departure_time": "07:00 PM",
"extras": []
},
{
"id": 4,
"company_id": 2,
"route_id": 16,
"class": "VIP",
"fare": 35,
"weekday": "thursday",
"reporting_time": "06:00 PM",
"departure_time": "07:00 PM",
"extras": []
},
{
"id": 5,
"company_id": 2,
"route_id": 16,
"class": "VIP",
"fare": 35,
"weekday": "friday",
"reporting_time": "06:00 PM",
"departure_time": "07:00 PM",
"extras": []
},
{
"id": 6,
"company_id": 2,
"route_id": 16,
"class": "Business",
"fare": 35,
"weekday": "saturday",
"reporting_time": "06:00 PM",
"departure_time": "07:00 PM",
"extras": []
}
]
这就是我的 eloquent 模型返回数据的方式,我使用转换器格式化输出以隐藏某些字段等。
我想按 class
属性 对这些数据进行分组。这就是我希望数据的样子:
[
{
"class":"Standard",
"schedules":[
{
"id":1,
"company_id":2,
"route_id":16,
"fare":35,
"weekday":"monday",
"reporting_time":"06:00 PM",
"departure_time":"07:00 PM",
"extras":[
]
},
{
"id":2,
"company_id":2,
"route_id":16,
"fare":35,
"weekday":"tuesday",
"reporting_time":"06:00 PM",
"departure_time":"07:00 PM",
"extras":[
]
},
{
"id":3,
"company_id":2,
"route_id":16,
"fare":35,
"weekday":"wednesday",
"reporting_time":"06:00 PM",
"departure_time":"07:00 PM",
"extras":[
]
}
]
},
{
"class":"VIP",
"schedules":[
{
"id":4,
"company_id":2,
"route_id":16,
"fare":35,
"weekday":"thursday",
"reporting_time":"06:00 PM",
"departure_time":"07:00 PM",
"extras":[
]
},
{
"id":5,
"company_id":2,
"route_id":16,
"fare":35,
"weekday":"friday",
"reporting_time":"06:00 PM",
"departure_time":"07:00 PM",
"extras":[
]
}
]
},
{
"class":"Business",
"schedules":[
{
"id":6,
"company_id":2,
"route_id":16,
"fare":35,
"weekday":"saturday",
"reporting_time":"06:00 PM",
"departure_time":"07:00 PM",
"extras":[
]
}
]
}
]
如何在不改变 eloquent 查询或数据库结构的情况下执行此操作?有没有办法可以使用变压器来实现这一目标?如果没有,我唯一的选择是更改 eloquent 查询,我将如何做才能使数据看起来像我想要的那样?
$result=[];
foreach($youarray as $key => $item)
{
$result[ $item['class'] ]['class'] = $item['class'];
$result[ $item['class'] ]['schedules'] = [$item['id'], $item['company_id'], etc. ];
}
return $result;