gpt4 book ai didi

php - 如何在不覆盖任何值的情况下合并两个集合

转载 作者:行者123 更新时间:2023-12-05 01:44:01 28 4
gpt4 key购买 nike

我试图在不覆盖任何值的情况下获取两个集合的并集:

第一个集合:

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() 但它会覆盖现有的键并且不会采用第一个集合中不存在的键。

任何帮助将不胜感激

最佳答案

你用过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);

Note: if you use this on a collection without nested array then you might get different result from this one.

关于php - 如何在不覆盖任何值的情况下合并两个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47698888/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com