Reputation: 326
I have two collections with data to combine:
//1
[
[
"date" => "2019-02-15",
"clicks" => 1,
],
]
//2
[
[
"date" => "2019-02-15",
"users" => 1,
],
]
it is necessary that the collection elements (arrays) that have the same values for a particular key are combined
Upvotes: 1
Views: 3569
Reputation: 326
<?php
$c1 = collect([
[
"date" => "2019-02-15",
"clicks" => 1,
],
[
"date" => "2019-02-16",
"clicks" => 3,
],
]);
$c2 = collect([
[
"date" => "2019-02-15",
"users" => 4,
],
[
"date" => "2019-02-16",
"users" => 5,
],
]);
$c1->merge($c2)
->groupBy('date')
->map(function ($items) {
return Arr::collapse($items);
});
Upvotes: 5
Reputation: 1
$collectionOne = new Collection(['foo']);
$collectionTwo = new Collection(['bar']);
$collection = $collectionOne->merge($collectionTwo);
Upvotes: -1