阅读(4484) (3)

Laravel 8 mergeRecursive() {#collection-method}

2021-07-01 15:42:01 更新

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]