By John Koster
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 behavior of the flip
method is similar to PHP's array_flip
function.
#Signature
1public function flip();
#Example Use
The following code example demonstrates the effects of the flip
method:
1use Illuminate\Support\Collection;
2
3// Create a new collection instance.
4$collection = new Collection([
5 'first' => 'I am first',
6 'second' => 'I am second',
7 'third' => 'I am third'
8]);
9
10// Flip the original collection.
11$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)
2 protected 'items' =>
3 array
4 'I am first' => string 'first'
5 'I am second' => string 'second'
6 'I am third' => string 'third'
∎