Laravel Collection – Using splice() Method to Remove Items

Laravel Collection - Using splice() Method to Remove Items from a Collection

Laravel Collections ( 11 Lessons )

Laravel Collections is one of the most powerful features of Laravel. Basically, collections are PHP arrays but it’s an Object Oriented approach to deal with PHP arrays.

see full series
  1. Laravel Collections – Introduction to Collections
  2. Laravel Collections – make Method
  3. Laravel Collections – wrap() and unwrap() Methods
  4. Laravel Collections – map() Method
  5. Laravel Collections – times() Method
  6. Laravel Collections – collapse() Method
  7. Laravel Collections – contains() & containsStrict() Methods
  8. Laravel Collections – each() Method
  9. Laravel Collection – Using toJson() Method
  10. Laravel Collection – Filtering Collection Items using where()
  11. 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. 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.

/**
 * 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:

$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.

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.

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.

$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.

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

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

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.

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.

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

When sharing a code snippet please wrap you code with pre tag and add a class code-block to it like below.
<pre class="code-block">you code here</pre>

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Your little help will keep this site alive and help us to produce quality content for you.