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',
]
*/
Copyright © http://blog.webfsd.com 2018 all right reserved,powered by Gitbook该文件修订时间: 2019-05-21 04:55:26

results matching ""

    No results matching ""