Differences between map and mapWithKeys in Laravel collections

Mr shahkhajeh
3 min readJan 21, 2023

--

mapWithKeys is a method available in Laravel collections that allows you to iterate over the items in the collection and create a new collection with the keys and values transformed by a callback function.

The method takes a callback function as its argument, which is called for each item in the collection. The callback function takes an item as its argument and should return an array with one or more key-value pairs. These key-value pairs will be used to create the new collection.

Here is an example:

$collection = collect([
['id' => 1, 'name' => 'Item 1'],
['id' => 2, 'name' => 'Item 2'],
['id' => 3, 'name' => 'Item 3'],
]);

$newCollection = $collection->mapWithKeys(function ($item) {
return [$item['id'] => $item['name']];
});

In this example, mapWithKeys is called on the $collection, which is a collection of arrays, and a callback function is passed to it. The callback function takes in each item of the collection, and returns an array containing the id of the item as the key and the name of the item as the value. The returned collection will have this key-value pairs.

It’s important to note that the mapWithKeys method does not modify the original collection, it returns a new collection containing the transformed items with new keys. The returned collection is an instance of Illuminate\Support\Collection, which has all the usual collection methods available on it.

mapWithKeys is useful when you need to map the items of a collection to a new collection with different keys, and you can use it to create a new collection with different keys but with the same values or vice versa. It's useful in situations where you want to create a collection with keys that are derived from the values of the original collection, such as when creating a collection of items with keys as the ids of the items, or when you want to map the items of a collection to a new collection with different keys, but with the same values.

difference between map and mapWithKeys in laravel collection

Both map and mapWithKeys are methods available in Laravel collections that allow you to iterate over the items in a collection and perform a callback function on each item. However, they differ in how they return the transformed items.

  • map method returns a new collection containing the transformed items. The transformed items are the return values of the callback function passed to the map method. The callback function takes an item as its argument and should return the transformed item.
$collection = collect([1, 2, 3, 4, 5]);
$newCollection = $collection->map(function ($item) {
return $item * 2;
});

In this example, the map method is called on the $collection, which is a collection of numbers, and a callback function is passed to it. The callback function takes in each item of the collection, multiplies it by 2, and returns the result. The returned collection will contain the new values, [2, 4, 6, 8, 10].

  • mapWithKeys method returns a new collection containing the transformed items with new keys. The transformed items are the key-value pairs returned by the callback function passed to the mapWithKeys method. The callback function takes an item as its argument and should return an array with one or more key-value pairs. These key-value pairs will be used to create the new collection.
$collection = collect([
['id' => 1, 'name' => 'Item 1'],
['id' => 2, 'name' => 'Item 2'],
['id' => 3, 'name' => 'Item 3'],
]);

$newCollection = $collection->mapWithKeys(function ($item) {
return [$item['id'] => $item['name']];
});

In this example, mapWithKeys is called on the $collection, which is a collection of arrays, and a callback function is passed to it. The callback function takes in each item of the collection, and returns an array containing the id of the item as the key and the name of the item as the value. The returned collection will have this key-value pairs.

In summary, The main difference between map and mapWithKeys is that map returns a new collection with the transformed items, while mapWithKeys returns a new collection with the transformed items and new keys. map is useful

--

--

Mr shahkhajeh
0 Followers

Laravel developer with a solid understanding of PHP . Strong problem-solving skills and ability to work well in a team environment