April 22, 2018 —John Koster
The pop
method is used to retrieve the last item from the collection while also removing it from the collection. If there are no items in the collection, the pop
method will return null
. The pop
method modifies the collection instance it is invoked on.
The pop
exhibits similar behavior to PHP's array_pop
function.
1public function pop();
The following sample demonstrates the use of the pop
method:
1use Illuminate\Support\Collection;2 3// Create a new collection instance.4$collection = new Collection([5 'first', 'second', 'third'6]);7 8// third9$third = $collection->pop();
The pop
method alters the original collection, so the $collection
variable would now contain a value similar to the following output:
1object(Illuminate\Support\Collection)2 protected 'items' =>3 array4 0 => string 'first'5 1 => string 'second'
∎
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.