April 22, 2018 —John Koster
The reverse
method is used to reverse the order of items in a collection. The reverse
method returns a new Collection
instance. The reverse
method does not preserve numerical keys but will preserve non-numerical keys.
The reverse
method is similar in behavior to PHP's array_reverse
function.
1public function reverse();
The following code example highlights the usage of the reverse
method:
1use Illuminate\Support\Collection;2 3// Create a new collection instance.4$collection = new Collection([5 'first', 'second', 'third'6]);7 8$reversed = $collection->reverse();
The $reversed
variable would now hold an instance of Collection
and would contain a value similar to the following output:
1object(Illuminate\Support\Collection)2 protected 'items' =>3 array4 0 => string 'third'5 1 => string 'second'6 2 => string 'first'
∎
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.