Laravel 5 Collections: Removing a Portion of a Collection With splice

4 min read

The splice method is a useful method; it is often used to remove a portion of a collection and return the removed section. It defines three parameters: $offset, $length and $replacement. The $offset tells the splice method where to begin when removing items from the collection. For example, if the $offset is 3, the all items beginning with the third item in the collection will be removed. If the $offset is negative, items will be removed that are that distance from the end of the collection. The splice method modifies the original Collection instance and returns the removed items as a new Collection instance.

The splice method is similar in behavior to PHP's array_splice function.

Signature

1public function splice(
2 $offset,
3 $length = null,
4 $replacement = []
5);

Example Use

The following example shows the basic usage of the splice method:

1use Illuminate\Support\Collection;
2 
3// Create a new collection instance.
4$collection = new Collection([
5 0, 1, 2 ,3 , 4, 5, 6, 7 ,8
6]);
7 
8// Splice the $collection starting with the third item.
9$spliced = $collection->splice(3);

After the above has executed, the $spliced variable will be an instance of Collection and contain a value similar to the following output:

1object(Illuminate\Support\Collection)
2 protected 'items' =>
3 array
4 0 => int 3
5 1 => int 4
6 2 => int 5
7 3 => int 6
8 4 => int 7
9 5 => int 8

The $collection variable will also have been modified, and will now contain a value similar to the following output:

1object(Illuminate\Support\Collection)
2 protected 'items' =>
3 array
4 0 => int 0
5 1 => int 1
6 2 => int 2

The $length parameter can be used to control how long the section that is removed from the collection can be. The following example demonstrates how to remove four items from the collection, starting with the third item:

1use Illuminate\Support\Collection;
2 
3// Create a new collection instance.
4$collection = new Collection([
5 0, 1, 2 ,3, 4 , 5, 6, 7 ,8
6]);
7 
8// Splice the $collection starting with the third item
9// and take at most 4 items.
10$spliced = $collection->splice(3, 4);

After the above example has executed, the $spliced variable will again be an instance of Collection and contain a value similar to the following output:

1object(Illuminate\Support\Collection)
2 protected 'items' =>
3 array
4 0 => int 3
5 1 => int 4
6 2 => int 5
7 3 => int 6

Like before, the $collection variable will have been modified and now contain a value similar to the following output:

1object(Illuminate\Support\Collection)
2 protected 'items' =>
3 array
4 0 => int 0
5 1 => int 1
6 2 => int 2
7 3 => int 7
8 4 => int 8

The $replacement parameter can be utilized to replace the items removed from the collection. When using the $replacement parameter, the items removed from the collection are still returned as a new instance of Collection. Any argument passed for $replacement must be an array, or must be able to be cast into an array. Any keys from the $replacement array will not be preserved when added to the collection.

The following example shows how to use the $replacement parameter:

1use Illuminate\Support\Collection;
2 
3// Create a new collection instance.
4$collection = new Collection([
5 'first', 'second', 'third', 'fourth'
6]);
7 
8// Replace all items beginning with the second.
9$spliced = $collection->splice(1, 3, [
10 '2nd', '3rd', '4th'
11]);

After the above code has executed, the $spliced variable will be an instance of Collection and contain a value similar to the following output:

1object(Illuminate\Support\Collection)
2 protected 'items' =>
3 array
4 0 => string 'second'
5 1 => string 'third'
6 2 => string 'fourth'

The $collection variable will also have been modified, but instead of just removing items, the new items 2nd, 3rd and 4th will have been inserted into the collection in place of the items that were removed:

1object(Illuminate\Support\Collection)
2 protected 'items' =>
3 array
4 0 => string 'first'
5 1 => string '2nd'
6 2 => string '3rd'
7 3 => string '4th'

Thanks for taking the time to read this post! If you found this article useful and want to help support more work like this, please consider sponsoring my work on GitHub, or by checking out some merch.

 Sponsor on GitHub  Shop Merch