Laravel

Laravel Collection - Using splice() Method to Remove Items

Laravel Collection splice method is very useful when you want to remove a portion of a collection and return the removed section.

23 January 2020Updated 23 April 20263 min read640 words
Laravel Collection - Using splice() Method to Remove Items from a Collection
Last updated:

Laravel Collection splice method is very useful when you want to remove a portion of a collection and return the removed section. This method takes three parameters: $offset, $length and $replacement. The $offset dictates the splice method where to begin when removing the items from a collection.

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 and return the removed items as a new Laravel Collection.

The splice method is very similar to the PHP's native array_splice method.

Signature

The signature of splice() method in the Illuminate/Support/Collection class looks something like below.

php
/**
 * Splice a portion of the underlying collection array.
 *
 * @param  int  $offset
 * @param  int|null  $length
 * @param  mixed  $replacement
 * @return static
 */
public function splice($offset, $length = null, $replacement = [])
{
    // ...
}

Examples

The below example shows a very simple use of the splice() method:

php
$collection = new Collection([
    0,1,2,3,4,5,6,7,8,9
]);

// Splice the $collection from 5th index, which is 4
$spliced = $collection->splice(5);

After executing the above code, the splice method will return a new collection of remaining items like below.

php
Illuminate\Support\Collection^ {#1338
  #items: array:5 [
    0 => 5
    1 => 6
    2 => 7
    3 => 8
    4 => 9
  ]
}

Now, if you dump the $collection variable, it will hold only the first five values of the original collection.

php
Illuminate\Support\Collection^ {#1341
  #items: array:5 [
    0 => 0
    1 => 1
    2 => 2
    3 => 3
    4 => 4
  ]
}

The $length parameter can be used to control how long the section that is removed from the collection can be. The following example shows, how you can remove three items from the collection.

php
$collection = new Collection([
    0,1,2,3,4,5,6,7,8,9
]);

// Splice the collection starting from 2nd item
// and take at most 3 items
$spliced = $collection->splice(2,3);

After executing the above example, you will get the below result.

php
Illuminate\Support\Collection {#656 ▼
  #items: array:3 [▼
    0 => 2
    1 => 3
    2 => 4
  ]
}

Like before, the $collection variable will now have the remaining items.

php
Illuminate\Support\Collection {#670 ▼
  #items: array:7 [▼
    0 => 0
    1 => 1
    2 => 5
    3 => 6
    4 => 7
    5 => 8
    6 => 9
  ]
}

The $replacement parameter can be used to replace the items in array after splitting it. After using the $replacement parameter, the items removed from the array will be returned as a instance of a collection.

Any argument passed for the $replacement must be an array and keys from the $replacement array will not be preserved when splitting the collection.

The below example shows how you can use the $replacement parameter.

php
use Illuminate\Support\Collection;

$collection = new Collection([
    'London', 'Paris', 'Dublin', 'Berlin'
]);

$spliced = $collection->splice(1, 3, [
    'New York', 'Tokyo', 'Sydney'
]);

In the above example we are splitting by starting from first item and then taking out three items from the collection while passing some more cities to the collection.

If you execute the above code and dump the $spliced collection, you will get the below output.

php
Illuminate\Support\Collection {#656 ▼
  #items: array:3 [▼
    0 => "Paris"
    1 => "Dublin"
    2 => "Berlin"
  ]
}

As you can see London has been removed from the collection and you have remaining three items. Now if you dump the $collection variable which is the original collection, you will see that the three new cities passed to the collection in spliced method are added to the original collection.

php
Illuminate\Support\Collection {#670 ▼
  #items: array:4 [▼
    0 => "London"
    1 => "New York"
    2 => "Tokyo"
    3 => "Sydney"
  ]
}

If you have any question about the splice() method, please leave it in the comments box below.