将两个不同的对象放在一起



我有来自CampusGroup$campus_groups

$campus_groups = CampusGroup::where('campus_id', $campus_id)->get();

其中有

[
{"id":1,"campus_id":1,"group_id":1,"created_at":"2021-03-15T08:15:27.000000Z","updated_at":"2021-03-15T08:15:27.000000Z"},
{"id":2,"campus_id":1,"group_id":2,"created_at":"2021-03-15T08:15:37.000000Z","updated_at":"2021-03-15T08:15:37.000000Z"},
{"id":3,"campus_id":1,"group_id":5,"created_at":"2021-03-15T08:16:49.000000Z","updated_at":"2021-03-15T08:16:49.000000Z"}
]

$campus_org_groups来自CampusOrganizationGroup

$campus_org_groups = CampusOrganizationGroup::where('campus_id', $campus_id)->get();

其中有

[
{"id":1,"campus_id":1,"campus_organization_id":1,"group_id":3,"created_at":"2021-03-15T08:15:50.000000Z","updated_at":"2021-03-15T08:15:50.000000Z"},
{"id":2,"campus_id":1,"campus_organization_id":1,"group_id":4,"created_at":"2021-03-15T08:15:55.000000Z","updated_at":"2021-03-15T08:15:55.000000Z"},
{"id":3,"campus_id":1,"campus_organization_id":1,"group_id":6,"created_at":"2021-03-15T08:17:28.000000Z","updated_at":"2021-03-15T08:17:28.000000Z"}
]

并希望像这样将两者放在一起(重要的关键是group_id,如果遗漏campus_organization_id我没关系)

[
{"id":1,"campus_id":1,"group_id":1,"created_at":"2021-03-15T08:15:27.000000Z","updated_at":"2021-03-15T08:15:27.000000Z"},
{"id":2,"campus_id":1,"group_id":2,"created_at":"2021-03-15T08:15:37.000000Z","updated_at":"2021-03-15T08:15:37.000000Z"},
{"id":3,"campus_id":1,"group_id":5,"created_at":"2021-03-15T08:16:49.000000Z","updated_at":"2021-03-15T08:16:49.000000Z"},
{"id":1,"campus_id":1,"group_id":3,"created_at":"2021-03-15T08:15:50.000000Z","updated_at":"2021-03-15T08:15:50.000000Z"},
{"id":2,"campus_id":1,"group_id":4,"created_at":"2021-03-15T08:15:55.000000Z","updated_at":"2021-03-15T08:15:55.000000Z"},
{"id":3,"campus_id":1,"group_id":6,"created_at":"2021-03-15T08:17:28.000000Z","updated_at":"2021-03-15T08:17:28.000000Z"}
]

如果我合并它们

$obj_merged = $campus_groups->merge($campus_org_groups);

这就是我得到的

[
{"id":1,"campus_id":1,"campus_organization_id":1,"group_id":3,"created_at":"2021-03-15T08:15:50.000000Z","updated_at":"2021-03-15T08:15:50.000000Z"},
{"id":2,"campus_id":1,"campus_organization_id":1,"group_id":4,"created_at":"2021-03-15T08:15:55.000000Z","updated_at":"2021-03-15T08:15:55.000000Z"},
{"id":3,"campus_id":1,"campus_organization_id":1,"group_id":6,"created_at":"2021-03-15T08:17:28.000000Z","updated_at":"2021-03-15T08:17:28.000000Z"}
]

如果我联合他们

$obj_merged = $campus_groups->union($campus_org_groups);

这就是我得到的

[
{"id":1,"campus_id":1,"group_id":1,"created_at":"2021-03-15T08:15:27.000000Z","updated_at":"2021-03-15T08:15:27.000000Z"},   
{"id":2,"campus_id":1,"group_id":2,"created_at":"2021-03-15T08:15:37.000000Z","updated_at":"2021-03-15T08:15:37.000000Z"},
{"id":3,"campus_id":1,"group_id":5,"created_at":"2021-03-15T08:16:49.000000Z","updated_at":"2021-03-15T08:16:49.000000Z"}
]

我已经改变了

$campus_groups = CampusGroup::where('campus_id', $campus_id)->get();
$campus_org_groups = CampusOrganizationGroup::where('campus_id', $campus_id)->get();

$campus_groups = collect(CampusGroup::where('campus_id', $campus_id)->get(['id','campus_id','group_id','created_at','updated_at'])); 
$campus_org_groups = collect(CampusOrganizationGroup::where('campus_id', $campus_id)->get(['id','campus_id','group_id','created_at','updated_at']));

请注意collect()的用法以及我真正想要的字段的规范(省略campus_organization_id)。然后,初始合并

$obj_merged = $campus_groups->merge($campus_org_groups);

将返回所需的输出

[
{"id":1,"campus_id":1,"group_id":1,"created_at":"2021-03-15T08:15:27.000000Z","updated_at":"2021-03-15T08:15:27.000000Z"},
{"id":2,"campus_id":1,"group_id":2,"created_at":"2021-03-15T08:15:37.000000Z","updated_at":"2021-03-15T08:15:37.000000Z"},
{"id":3,"campus_id":1,"group_id":5,"created_at":"2021-03-15T08:16:49.000000Z","updated_at":"2021-03-15T08:16:49.000000Z"},
{"id":1,"campus_id":1,"group_id":3,"created_at":"2021-03-15T08:15:50.000000Z","updated_at":"2021-03-15T08:15:50.000000Z"},
{"id":2,"campus_id":1,"group_id":4,"created_at":"2021-03-15T08:15:55.000000Z","updated_at":"2021-03-15T08:15:55.000000Z"},
{"id":3,"campus_id":1,"group_id":6,"created_at":"2021-03-15T08:17:28.000000Z","updated_at":"2021-03-15T08:17:28.000000Z"}
]

你能测试这个解决方案吗:

$obj_merged = (object) array_merge((array) $campus_groups, (array) $campus_org_groups);

这是非常简单的操作,array_merge应该可以工作

<?php $campus_groups = [
['id' => 1, 'campus_id'=> 1, 'group_id'=> 1],
['id' => 2, 'campus_id'=> 1, 'group_id'=> 2],
['id' => 3, 'campus_id'=> 1, 'group_id'=> 5],
];
$campus_org_groups = [
['id' => 1, 'campus_id'=> 1, 'group_id'=> 3, 'campus_organization_id' => 1],
['id' => 2, 'campus_id'=> 1, 'group_id'=> 4, 'campus_organization_id' => 1],
['id' => 3, 'campus_id'=> 1, 'group_id'=> 5, 'campus_organization_id' => 1],
];
print_r(array_merge($campus_groups , $campus_org_groups));

https://repl.it/@technoknol/StainedHealthyBlogclient

我用array_push(文档)做了一些简单的事情,例如:

foreach($array2 as $val){    
array_push($array1, $val);
}

在这里用下面的代码进行了测试:

<?php
class newObject{
public $id;
public $campus_id;
public $group_id;
public $created_at;
public $updated_at;

function create($id, $campus_id, $group_id, $created_at, $updated_at){
$this->id = $id;
$this->campus_id = $campus_id;
$this->group_id = $group_id;
$this->created_at = $created_at;
$this->updated_at = $updated_at;

}

}
class newObject2{
public $id;
public $campus_id;
public $campus_organization_id;
public $group_id;
public $created_at;
public $updated_at;

function create($id, $campus_id, $campus_organization_id, $group_id, $created_at, $updated_at){
$this->id = $id;
$this->campus_id = $campus_id;
$this->campus_organization_id = $campus_organization_id;
$this->group_id = $group_id;
$this->created_at = $created_at;
$this->updated_at = $updated_at;

}

}
$newObject = new newObject;
$newObject->create("1", "1", "1", "2021-03-15T08:15:27.000000Z", "2021-03-15T08:15:27.000000Z");
$newObjectA = new newObject;
$newObjectA->create("2", "1", "1", "2021-03-15T08:15:27.000000Z", "2021-03-15T08:15:27.000000Z");
$array1 = [$newObject, $newObjectA];
$newObject2 = new newObject2;
$newObject2->create("1", "1", "1", "3", "2021-03-15T08:15:50.000000Z", "2021-03-15T08:15:50.000000Z");
$newObject2A = new newObject2;
$newObject2A->create("2", "1", "1", "3", "2021-03-15T08:15:50.000000Z", "2021-03-15T08:15:50.000000Z");
$array2 = [$newObject2, $newObject2A];
foreach($array2 as $val){

array_push($array1, $val);
}
var_dump($array1);

输出最终:

array(4) {
[0]=>
object(newObject)#1 (5) {
["id"]=>
string(1) "1"
["campus_id"]=>
string(1) "1"
["group_id"]=>
string(1) "1"
["created_at"]=>
string(27) "2021-03-15T08:15:27.000000Z"
["updated_at"]=>
string(27) "2021-03-15T08:15:27.000000Z"
}
[1]=>
object(newObject)#2 (5) {
["id"]=>
string(1) "2"
["campus_id"]=>
string(1) "1"
["group_id"]=>
string(1) "1"
["created_at"]=>
string(27) "2021-03-15T08:15:27.000000Z"
["updated_at"]=>
string(27) "2021-03-15T08:15:27.000000Z"
}
[2]=>
object(newObject2)#3 (6) {
["id"]=>
string(1) "1"
["campus_id"]=>
string(1) "1"
["campus_organization_id"]=>
string(1) "1"
["group_id"]=>
string(1) "3"
["created_at"]=>
string(27) "2021-03-15T08:15:50.000000Z"
["updated_at"]=>
string(27) "2021-03-15T08:15:50.000000Z"
}
[3]=>
object(newObject2)#4 (6) {
["id"]=>
string(1) "2"
["campus_id"]=>
string(1) "1"
["campus_organization_id"]=>
string(1) "1"
["group_id"]=>
string(1) "3"
["created_at"]=>
string(27) "2021-03-15T08:15:50.000000Z"
["updated_at"]=>
string(27) "2021-03-15T08:15:50.000000Z"
}
}

相关内容

  • 没有找到相关文章

最新更新