Php 合并共享相同id的数组

Php 合并共享相同id的数组,php,mysql,laravel,Php,Mysql,Laravel,目前我有这样的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",

目前我有这样的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"
        }
      }
    }
  }
]
[
  {
    "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"
        }
      }
    }
  }
]
按功能实现:

$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();
我需要的是,如果阵列共享相同的
房间id
,它将与多个
用户合并为一个阵列,如下所示:

[
  {
    "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"
        }
      }
    }
  }
]
[
  {
    "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>";
$data=json\u decode($json);
$newFormat=[];
foreach($每个数据为$){
如果(!isset($newFormat[$each->room_id]){
$newFormat[$each->room\u id]=$each;
}否则{
$newFormat[$each->room_id]->users[]=$each->users[0];
}
}
“回声”。var_导出($newFormat,true)。"";

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

此代码将帮助您实现目标

$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>";
$data=json\u decode($json);
$newFormat=[];
foreach($每个数据为$){
如果(!isset($newFormat[$each->room_id]){
$newFormat[$each->room\u id]=$each;
}否则{
$newFormat[$each->room_id]->users[]=$each->users[0];
}
}
“回声”。var_导出($newFormat,true)。"";

这将按照您的期望显示准确的结果

值得检查他们想要用于代码的语言。值得检查他们想要用于代码的语言。