How to flip keys with values in Laravel collection?

< 1 min read

To flip the keys with the values in a Laravel collection, you can use the flip method. This method will create a new collection with the keys and values swapped.

Here’s an example:

$collection = collect(['name' => 'bob', 'job' => 'developer']);
$flipped = $collection->flip();

// $flipped = ['bob' => 'name', 'developer' => 'job']

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.