Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

change key value laravel map collection

// Using mapWithKeys
$collection = collect([
    [
        'name' => 'John',
        'department' => 'Sales',
        'email' => 'john@example.com',
    ],
    [
        'name' => 'Jane',
        'department' => 'Marketing',
        'email' => 'jane@example.com',
    ]
]);
 
$keyed = $collection->mapWithKeys(function ($item, $key) {
    return [$item['email'] => $item['name']];
});
 
$keyed->all();
 
/*
    [
        'john@example.com' => 'John',
        'jane@example.com' => 'Jane',
    ]
*/
Source by laravel.com #
 
PREVIOUS NEXT
Tagged: #change #key #laravel #map #collection
ADD COMMENT
Topic
Name
9+9 =