mapWithKeys方法
遍历集合并将每个值传入给定的回调。回调应该返回包含一个键值对的关联数组
$collection = collect([
[
'name' => 'John',
'department' => 'Sales',
'email' => '[email protected]'
],
[
'name' => 'Jane',
'department' => 'Marketing',
'email' => '[email protected]'
]
]);
$keyed = $collection->mapWithKeys(function ($item) {
return [$item['email'] => $item['name']];
});
$keyed->all();
/*
[
'[email protected]' => 'John',
'[email protected]' => 'Jane',
]
*/