1 回答

TA貢獻(xiàn)1843條經(jīng)驗 獲得超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));
結(jié)果:
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 方法將給定的數(shù)組或集合遞歸地與原始集合合并。如果給定項中的字符串鍵與原始集合中的字符串鍵匹配,則這些鍵的值將合并到一個數(shù)組中,這是遞歸完成的:
$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 關(guān)注
- 151 瀏覽
添加回答
舉報