mapWithKeys在laravel,我不明白它是怎么工作的



我看到了laravel的例子,但我不明白它是如何工作的。

例如:

$collection = collect([
[
'name' => 'John',
'department' => 'Sales',
'email' => 'john@example.com'
],
[
'name' => 'Jane',
'department' => 'Marketing',
'email' => 'jane@example.com'
]
]);
$keyed = $collection->mapWithKeys(function ($item) {
return [$item['email'] => $item['name']];
});
$keyed->all();

有人能解释一下细节吗?

$collection = collect([
[
'name' => 'John',
'department' => 'Sales',
'email' => 'john@example.com'
],
[
'name' => 'Jane',
'department' => 'Marketing',
'email' => 'jane@example.com'
]
]);
$keyed = $collection->mapWithKeys(function ($item) {
//this line takes one array of collection object in item array and  make a key of its email and store name on that email key  
return [$item['email'] => $item['name']];
});
$keyed->all();

相关内容

最新更新