gpt4 book ai didi

php - 合并共享相同 id 的数组

转载 作者:行者123 更新时间:2023-11-30 21:28:55 25 4
gpt4 key购买 nike

目前我有这样的 JSON:

[
{
"user_id": 6,
"room_id": 2,
"date_from": "2019-09-15 00:00:00",
"date_to": "2020-09-01 00:00:00",
"created_at": null,
"updated_at": null,
"users": [
{
"id": 6,
"name": "Makenna",
"surname": "Boyle",
"email": "nparker@yahoo.com",
"university_id": 3
}
],
"room": {
"id": 2,
"room_number": 327,
"floor": 3,
"slots": 2,
"dorm_id": 1,
"price": 0,
"features": "[{\"feature\":\"Nemokamas WiFi\",\"feature\":\"Erdvus dušas\"}]",
"photos": "[{\"img\":\"\\/images\\/rooms\\/9_1_5d3f6735884627.19832875.jpg\"},{\"img\":\"\\/images\\/rooms\\/9_1_5d3f673589a070.51161328.jpg\"},{\"img\":\"\\/images\\/rooms\\/9_1_5d3f67358b0419.09559739.jpg\"}]",
"created_at": null,
"updated_at": null,
"dorm": {
"id": 1,
"name": "Statybininkų prospekto bendrabutis",
"university_id": 2,
"photo": "https://www.umt.edu/housing/rh/descriptions/photos/Miller%20West%20Side%20View.jpg",
"university": {
"id": 2,
"name": "Klaipėdos Universitetas",
"city": "Klaipėda"
}
}
}
},
{
"user_id": 8,
"room_id": 2,
"date_from": "2019-09-15 00:00:00",
"date_to": "2020-09-01 00:00:00",
"created_at": null,
"updated_at": null,
"users": [
{
"id": 8,
"name": "Kassandra",
"surname": "Blick",
"email": "fschuppe@armstrong.com",
"university_id": 2
}
],
"room": {
"id": 2,
"room_number": 327,
"floor": 3,
"slots": 2,
"dorm_id": 1,
"price": 0,
"features": "[{\"feature\":\"Nemokamas WiFi\",\"feature\":\"Erdvus dušas\"}]",
"photos": "[{\"img\":\"\\/images\\/rooms\\/9_1_5d3f6735884627.19832875.jpg\"},{\"img\":\"\\/images\\/rooms\\/9_1_5d3f673589a070.51161328.jpg\"},{\"img\":\"\\/images\\/rooms\\/9_1_5d3f67358b0419.09559739.jpg\"}]",
"created_at": null,
"updated_at": null,
"dorm": {
"id": 1,
"name": "Statybininkų prospekto bendrabutis",
"university_id": 2,
"photo": "https://www.umt.edu/housing/rh/descriptions/photos/Miller%20West%20Side%20View.jpg",
"university": {
"id": 2,
"name": "Klaipėdos Universitetas",
"city": "Klaipėda"
}
}
}
}
]

功能实现:

$rooms = OccupiedRoom::with([
'users' => function ($query) {
$query->select(['id', 'name', 'surname', 'email', 'university_id']);
},
'room.dorm' => function ($query) {
$query->select('dorms.id', 'dorms.name', 'dorms.university_id', 'dorms.photo');
},
'room.dorm.university' => function ($query) {
$query->select('universities.id', 'universities.name', 'universities.city');
},
])->get();

我需要的是,如果数组共享相同的 room_id,它将合并到一个包含多个 users 的数组中,如下所示:

[
{
"room_id": 2,
"date_from": "2019-09-15 00:00:00",
"date_to": "2020-09-01 00:00:00",
"created_at": null,
"updated_at": null,
"users": [
{
"id": 6,
"name": "Makenna",
"surname": "Boyle",
"email": "nparker@yahoo.com",
"university_id": 3
},
{
"id": 8,
"name": "Kassandra",
"surname": "Blick",
"email": "fschuppe@armstrong.com",
"university_id": 2
}
],
"room": {
"id": 2,
"room_number": 327,
"floor": 3,
"slots": 2,
"dorm_id": 1,
"price": 0,
"features": "[{\"feature\":\"Nemokamas WiFi\",\"feature\":\"Erdvus dušas\"}]",
"photos": "[{\"img\":\"\\/images\\/rooms\\/9_1_5d3f6735884627.19832875.jpg\"},{\"img\":\"\\/images\\/rooms\\/9_1_5d3f673589a070.51161328.jpg\"},{\"img\":\"\\/images\\/rooms\\/9_1_5d3f67358b0419.09559739.jpg\"}]",
"created_at": null,
"updated_at": null,
"dorm": {
"id": 1,
"name": "Statybininkų prospekto bendrabutis",
"university_id": 2,
"photo": "https://www.umt.edu/housing/rh/descriptions/photos/Miller%20West%20Side%20View.jpg",
"university": {
"id": 2,
"name": "Klaipėdos Universitetas",
"city": "Klaipėda"
}
}
}
}
]

最佳答案

此代码将帮助您实现目标。

$data = json_decode($json);

$newFormat = [];
foreach ($data AS $each) {
if (!isset($newFormat[$each->room_id])) {
$newFormat[$each->room_id] = $each;
} else {
$newFormat[$each->room_id]->users[] = $each->users[0];
}
}

echo "<pre>" . var_export($newFormat, true) . "</pre>";

这将显示符合您期望的准确结果

关于php - 合并共享相同 id 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57348139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com