By John Koster
reverse
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 following code example highlights the usage of the reverse
method:
1<?php
2
3use Illuminate\Support\Collection;
4
5// Create a new collection instance.
6$collection = new Collection([
7 'first', 'second', 'third'
8]);
9
10$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)[134]
2 protected 'items' =>
3 array (size=3)
4 0 => string 'third' (length=5)
5 1 => string 'second' (length=6)
6 2 => string 'first' (length=5)
∎