1 回答
TA贡献1843条经验 获得超7个赞
您可以为此使用mergeRecursive :
$array1 = [
"data_to_merge" => ["2020-03-11 16:00:00", "2020-03-24 14:00:00"],
"data_to_merge_past_year" => ["2019-03-11 16:00:00"],
];
$array2 = [
"data_to_merge" => [],
"data_to_merge_past_year" => ["2018-03-11 14:00:00"],
];
$result = collect($array1)->mergeRecursive(collect($array2));
结果:
Illuminate\Support\Collection {#1278 ▼
#items: array:2 [▼
"data_to_merge" => array:2 [▼
0 => "2020-03-11 16:00:00"
1 => "2020-03-24 14:00:00"
]
"data_to_merge_past_year" => array:2 [▼
0 => "2019-03-11 16:00:00"
1 => "2018-03-11 14:00:00"
]
]
}
从文档:
mergeRecursive 方法将给定的数组或集合递归地与原始集合合并。如果给定项中的字符串键与原始集合中的字符串键匹配,则这些键的值将合并到一个数组中,这是递归完成的:
$collection = collect(['product_id' => 1, 'price' => 100]);
$merged = $collection->mergeRecursive(['product_id' => 2, 'price' => 200, 'discount' => false]);
$merged->all();
// ['product_id' => [1, 2], 'price' => [100, 200], 'discount' => false]
- 1 回答
- 0 关注
- 132 浏览
添加回答
举报