use Illuminate\Support\Collection;
$collection1 = collect([
[
"имя1" => "John",
"объем1" => "100",
"мусор" => "x"
],
[
"имя1" => "Jane",
"объем1" => "200",
"мусор" => "y"
]
]);
$collection2 = collect([
[
"имя2" => "John",
"объем2" => "300",
"мусор" => "x"
],
[
"имя2" => "Jane",
"объем2" => "400",
"мусор" => "z"
]
]);
function normalizeKeys(Collection $collection, $nameKey, $volumeKey)
{
return $collection->map(function ($item) use ($nameKey, $volumeKey) {
return [
'name' => $item[$nameKey] ?? null,
'volume' => $item[$volumeKey] ?? null,
];
});
}
$normalized1 = normalizeKeys($collection1, 'имя1', 'объем1');
$normalized2 = normalizeKeys($collection2, 'имя2', 'объем2');
$result = $normalized1->map(function ($item1) use ($normalized2) {
$match = $normalized2->firstWhere('name', $item1['name']);
return [
'name' => $item1['name'],
'volume1' => $item1['volume'],
'volume2' => $match['volume'] ?? null,
];
});