将两个不同的对象放在一起
Get the two different objects together
我有 $campus_groups
来自 CampusGroup
$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"}
]
如果我merge他们
$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"}
]
如果我union他们
$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"}
]
你能测试这个解决方案吗:
$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));
我用 array_push
(doc) 做了一些简单的事情,比如:
foreach($array2 as $val){
array_push($array1, $val);
}
使用以下代码测试here:
<?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"
}
}
我变了
$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);
会return想要的输出
[
{"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"}
]
我有 $campus_groups
来自 CampusGroup
$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"}
]
如果我merge他们
$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"}
]
如果我union他们
$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"}
]
你能测试这个解决方案吗:
$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));
我用 array_push
(doc) 做了一些简单的事情,比如:
foreach($array2 as $val){
array_push($array1, $val);
}
使用以下代码测试here:
<?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"
}
}
我变了
$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);
会return想要的输出
[
{"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"}
]