@fylzero This could work
$result = $col1->map(function ($item) use ($col2) {
if ($col2->contains('status_slug', $item['slug'])) {
return $col2->firstWhere('status_slug', $item['slug']) + $item;
}
})->filter();
Is it possible to take this...
$col1 = collect([
[
'slug' => 'merp',
'label' => 'Merp',
'desc' => 'Merp of merp',
],
[
'slug' => 'derp',
'label' => 'Derp',
'desc' => 'Derp derpington',
],
[
'slug' => 'whoa',
'label' => 'Whoa There',
'desc' => 'Hang on just a second',
],
]);
...and this...
$col2 = collect([
[
'id' => 1,
'name' => 'Steve',
'status_slug' => 'merp',
'secret' => '12345',
],
[
'id' => 2,
'name' => 'Joe',
'status_slug' => 'derp',
'secret' => '67890',
],
]);
...and have the end result be this...
=> Illuminate\Support\Collection {
all: [
[
"id" => 1,
"name" => "Steve",
"status_slug" => "merp", // matched on slug
"secret" => "12345",
"slug" => "merp", // matched on status_slug
"label" => "Merp",
"desc" => "Merp of merp",
],
[
"id" => 2,
"name" => "Joe",
"status_slug" => "derp", // matched on slug
"secret" => "67890",
"slug" => "derp", // matched on status_slug
"label" => "Derp",
"desc" => "Derp derpington",
],
],
}
I'd assume I can just map or each this somehow but am struggling to get there.
@fylzero This could work
$result = $col1->map(function ($item) use ($col2) {
if ($col2->contains('status_slug', $item['slug'])) {
return $col2->firstWhere('status_slug', $item['slug']) + $item;
}
})->filter();
Please or to participate in this conversation.