我尝试将重复值连接到数组中,但是如果连接的数组具有不同的值,则应该自动创建另一个数组。
这是我的代码,在laravel上使用查询生成器
DB::table('users')
->select(
'users.id',
'users.first_name',
'users.last_name',
'users.no_rekening',
'users.empid',
'users.no_rekening',
'users.detail',
'users.is_approve',
'users.is_active',
'karyawan_project.project_id',
'project.nama as nama_project',
'client.nama as nama_client'
)->leftJoin(
'karyawan_project',
'users.id','=','karyawan_project.karyawan_id',
'client.nama '
)->join(
'project',
'karyawan_project.project_id','=','project.id'
)->join('spk','project.spk_id','=','spk.id')
->join('client','spk.client_id','=','client.id')
->where('tipe', 2)
->whereIn('is_active', [1,0,2])
->orderBy('users.id')
->get();
和结果
1 => {#747 ▼
+"id": 17
+"first_name": "Dadan R"
+"last_name": "Hadiansyah"
+"no_rekening": "1150006713467"
+"empid": "01180104"
+"detail": ""
+"is_approve": 1
+"is_active": 1
+"project_id": 4
+"nama_project": "Driver Manager & Operasional"
+"nama_client": "PT. TRANSPORTASI GAS INDONESIA"
}
2 => {#797 ▼
+"id": 18
+"first_name": "Setia"
+"last_name": "Darma"
+"no_rekening": "1150006713475"
+"empid": "01180105"
+"detail": null
+"is_approve": 1
+"is_active": 1
+"project_id": 181
+"nama_project": "Driver Operasional"
+"nama_client": "PT. TRANSPORTASI GAS INDONESIA"
}
3 => {#790 ▼
+"id": 18
+"first_name": "Setia"
+"last_name": "Darma"
+"no_rekening": "1150006713475"
+"empid": "01180105"
+"detail": null
+"is_approve": 1
+"is_active": 1
+"project_id": 4
+"nama_project": "Driver Manager & Operasional"
+"nama_client": "PT. TRANSPORTASI GAS INDONESIA"
}
4 => {#796 ▼
+"id": 18
+"first_name": "Setia"
+"last_name": "Darma"
+"no_rekening": "1150006713475"
+"empid": "01180105"
+"detail": null
+"is_approve": 1
+"is_active": 1
+"project_id": 6
+"nama_project": "PENGAMANAN STATION MBK"
+"nama_client": "PT. PERMATA GRAHA NUSANTARA"
}
5 => {#757 ▼
+"id": 19
+"first_name": "Ardian"
+"last_name": "Ismail"
+"no_rekening": "1150004433837"
+"empid": "01180106"
+"detail": null
+"is_approve": 1
+"is_active": 0
+"project_id": 4
+"nama_project": "Driver Manager & Operasional"
+"nama_client": "PT. TRANSPORTASI GAS INDONESIA"
结果具有重复的值,其中id = 18,并且具有不同的nama_project
我想要的是,具有重复值的id连接到一个数组中 以及具有不同值的nama_project
喜欢
1 => {#747 ▼
+"id": 17
+"first_name": "Dadan R"
+"last_name": "Hadiansyah"
+"no_rekening": "1150006713467"
+"empid": "01180104"
+"detail": ""
+"is_approve": 1
+"is_active": 1
+"project_id": 4
+"nama_project": "Driver Manager & Operasional"
+"nama_client": "PT. TRANSPORTASI GAS INDONESIA"
}
2 => {#797 ▼
+"id": 18
+"first_name": "Setia"
+"last_name": "Darma"
+"no_rekening": "1150006713475"
+"empid": "01180105"
+"detail": null
+"is_approve": 1
+"is_active": 1
+"project_id": 181
+"nama_project": ▼{
+"Driver Operasional"
+"Driver Manager & Operasional"
+"PENGAMANAN STATION MBK"
}
+"nama_client": "PT. TRANSPORTASI GAS INDONESIA"
}
答案 0 :(得分:1)
由于您已按users.id订购结果。假设您将结果保存在$users
中,则可以执行以下操作:
$usersWithMergedProjects = [];
$previous_id = 0;
foreach($users as $user){
if($user->id == $previous_id){
array_push($usersWithMergedProjects[$user->id]->nama_projects, $user->nama_project);
} else {
$usersWithMergedProjects[$user->id] = $user;
$usersWithMergedProjects[$user->id]->nama_projects = [];
array_push($usersWithMergedProjects[$user->id]->nama_projects, $user->nama_project);
}
$previous_id = $user->id;
unset($usersWithMergedProjects[$user->id]->nama_project);
}
注意:如果对此有任何错误,请对错误进行截图。
答案 1 :(得分:0)
我找到了答案
$data = DB::table('users')
// ->with(['history'=>function($kar){
// $kar->with('project.spk.client')->select('project_id','karyawan_id')->where('status',1);
// }])
->select('users.id','users.first_name','users.last_name','users.no_rekening','users.empid','users.no_rekening',
'users.detail','users.is_approve','users.is_active','karyawan_project.project_id','karyawan_project.status',
'project.nama as nama_project',
'client.description as nama_client')
->leftJoin('karyawan_project', 'users.id','=','karyawan_project.karyawan_id')
->join('project','karyawan_project.project_id','=','project.id')
->join('spk','project.spk_id','=','spk.id')
->join('client','spk.client_id','=','client.id')
->where('tipe', 2)->whereIn('is_active',[1,0,2])->orderBy('users.id')->get()
;
$previous_id = 0;
foreach($data as $key => $user){
$user->merged = [];
$user->project = '<b>'.$user->nama_client. '</b> ' . $user->nama_project;
if ($previous_id == $user->id) {
$minKey = $key-1;
if (!empty($data[$minKey]->merged)) {
if ($user->status == 1) {
array_push($user->merged, $user->project);
}
$user->merged = array_merge($user->merged, $data[$minKey]->merged);
}else {
if ($user->status == 1) {
array_push($user->merged, $user->project, $data[$minKey]->project);
}
}
unset($data[$minKey]);
}else {
if ($user->status == 1) {
array_push($user->merged,$user->project);
}
}
$previous_id = $user->id;
}
$data = $data->sortBy('first_name');