我试图获得两个集合的联合而不覆盖任何值:
第一集:
Collection {#246 ▼
#items: array [▼
0 => array [▼
"status" => "Invoice"
"letterPaymentAmount" => 4240.0
"BankAccountNo" => "SEK"
"Date" => "2017-07-15T16:32:22"
"DueDate" => "2017-07-28T00:00:00"
"ocr" => "3000000202"
"lastDatePayDay" => "28"
]]}
第二次收集
Collection {#246 ▼
#items: array [▼
0 => array [▼
"status": "Reminder",
"letterPaymentAmount": 4300,
"BankAccountNo": "SEK",
"Date": "2017-08-15T16:01:30",
"DueDate": "2017-08-30T00:00:00",
"ocr": "3000002406",
"lastDatePayDay": "30",
"lastDatePayMonth": "Aug",
"Country": "Singapore",
"Customer": "TickTack"
]]}
我想要完成这个:
Collection {#246 ▼
#items: array [▼
0 => array [▼
"status" => "Invoice"
"letterPaymentAmount" => 4240.0
"BankAccountNo" => "SEK"
"Date" => "2017-07-15T16:32:22"
"DueDate" => "2017-07-28T00:00:00"
"ocr" => "3000000202"
"lastDatePayDay" => "28"],
1=> [
"status": "Reminder",
"letterPaymentAmount": 4300,
"BankAccountNo": "SEK",
"Date": "2017-08-15T16:01:30",
"DueDate": "2017-08-30T00:00:00",
"ocr": "3000002406",
"lastDatePayDay": "30",
"lastDatePayMonth": "Aug",
"Country": "Singapore",
"Customer": "TickTack" ]]}
我不想覆盖任何现有的键或值,只需将它们添加到一起即可。
我尝试了union()
但是它会覆盖现有的密钥,并且不会获取第一个集合中不存在的密钥。
非常感谢任何帮助
答案 0 :(得分:3)
使用merge()
$first = collect(['One','Two']);
$second = collect(['Two','Five']);
$merged = $first->merge($second);
// $merged->all();
答案 1 :(得分:0)
使用push
在不编辑项目的情况下将新项目添加到现有馆藏。
$collection = collect([['a' => 'b']]);
$collection2 = collect([['c' => 'd']]);
$collection2->each(function ($e) use (&$collection) {
$collection->push($e);
});
答案 2 :(得分:0)
您使用过concat()
吗?一个例子如下:
$col1 = collect([
['abc' => 1, 'dec' => 2],
['abc' => 1, 'dec' => 6]
]);
$col2 = collect([
['abc' => 3, 'dec' => 4, 'acd' => 2],
['abc' => 1, 'dec' => 8, 'acd' => 6],
]);
return $col1->concat($col2);
注意:如果您在没有嵌套数组的集合上使用它,那么您可能会得到与此不同的结果。