November 30, 2016 —John Koster
slice($offset, $length = null, $preserveKeys = false)
The slice
method is used to return a slice, or portion of the collection starting at the given $offset
. The $offset
tells the slice
method where to begin when creating the collection. For example, if the $offset
is 3
, the new collection will begin with the third item in the collection. If the $offset
is negative, the new collection will start that distance from the end of the collection. The slice
method returns a new Collection
instance and will not modify the original Collection
instance.
The following code example demonstrates the usage of the slice
method using a simple collection:
1<?php 2 3use Illuminate\Support\Collection; 4 5// Create a new collection instance. 6$collection = new Collection([1, 2, 3, 4, 5 , 6, 7, 8]); 7 8// Slice the collection starting at the third item. 9// The returned collection will contain items10// 4, 5, ,7 and 8.11$sliced = $collection->slice(3);
After the above code has executed the $sliced
variable will be an instance of Collection
and contain a value similar to the following output:
1object(Illuminate\Support\Collection)[134]2 protected 'items' =>3 array (size=5)4 0 => int 45 1 => int 56 2 => int 67 3 => int 78 4 => int 8
The same result can be accomplished using a negative $offset
like so:
1<?php2 3// Slice the collection starting starting at the4// fifth item from the end of the collection. The5// returned collection will contain the items6// 4, 5, 6, 7 and 8.7$sliced = $collection->slice(-5);
After the above code has executed, like before, the $sliced
variable would be 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=5)4 0 => int 45 1 => int 56 2 => int 67 3 => int 78 4 => int 8
The $length
parameter allows developers to control the size of the returned Collection
. If the provided $length
is positive, the returned collection will have up that many items. The total number of items can be less than the supplied $length
if the original collection does not have enough items to satisfy the $length
. If the $length
is negative, the returned collection will stop that many items from the end of the original collection. The following example shows the various uses of the $length
parameter using the $collection
instance created earlier:
1<?php 2 3// The returned collection will contain the items 4// 2, 3 and 4. 5$sliced = $collection->slice(1, 3); 6 7// The returned collection will contain the items 8// 1, 2 and 3. 9$sliced = $collection->slice(0, 3);10 11// The returned collection will contain the items12// 1, 2, 3, 4 and 5.13$sliced = $collection->slice(0, -3);
By default, the slice
method will reorder and reset the keys of the returned collection's items. This can be changed by passing true
as the argument for the $preserveKeys
parameter. The following code samples show the affect of the $preserveKeys
parameter:
1<?php2 3// Slice the collection without preserving keys.4$sliced = $collection->slice(2, 3);5 6// Slice the collection while preserving keys.7$preservedKeySlice = $collection->slice(2, 3, true);
After the above code has executed, the $sliced
variable will be an instance of Collection
and contain a value similar to the following output:
1object(Illuminate\Support\Collection)[134]2 protected 'items' =>3 array (size=3)4 0 => int 35 1 => int 46 2 => int 5
Notice that the keys of the collection are 0
, 1
and 2
. The following output is for the $preservedKeySlice
variable (which is also an instance of Collection
). Notice how the keys are 2
, 3
and 4
, because they were preserved:
1object(Illuminate\Support\Collection)[135]2 protected 'items' =>3 array (size=3)4 2 => int 35 3 => int 46 4 => int 5
∎
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.