November 29, 2016 —John Koster
flip
The flip
method will return a new Collection
instance where are the all the collection item's keys have been exchanged with their corresponding values.
The following code example demonstrates the effects of the flip
method:
1<?php 2 3use Illuminate\Support\Collection; 4 5// Create a new collection instance. 6$collection = new Collection([ 7 'first' => 'I am first', 8 'second' => 'I am second', 9 'third' => 'I am third'10]);11 12// Flip the original collection.13$flippedCollection = $collection->flip();
The $flippedCollection
variable will now contain a new instance of the Collection
class with a structure similar to the following output:
1object(Illuminate\Support\Collection)[134]2 protected 'items' =>3 array (size=3)4 'I am first' => string 'first' (length=5)5 'I am second' => string 'second' (length=6)6 'I am third' => string 'third' (length=5)
∎
The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.