Search

Laravel Collection Public API: shift

November 30, 2016 —John Koster

shift

The shift method is used to remove the first item from the collection and return its value. The shift method modifies the collection instance. The following code example shows how to use the shift 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// first
11$firstValue = $collection->shift();

After the above code has executed, the first item from the collection will have been removed. The final value of the $collection variable would be similar to the following output:

1object(Illuminate\Support\Collection)[133]
2 protected 'items' =>
3 array (size=2)
4 0 => string 'second' (length=6)
5 1 => string 'third' (length=5)