Overview
ListCollection provides methods to remove elements. All removal methods automatically reindex the remaining elements to maintain sequential integer keys.forget()
Remove one or more elements by their indices and reindex the collection.Signature
A single index or array of indices to remove.
Examples
Remove single element
Remove multiple elements
Chained forget calls use reindexed keys
Out-of-bounds key is ignored
pull()
Remove and return an element by its index, reindexing the collection.Signature
The index of the element to remove.
Value to return if the key doesn’t exist. Can be a value or a closure.
TValue|TPullDefault - The removed value or default if key not found.
Examples
Pull existing element
Pull with default value
Pull with closure default
offsetUnset()
Remove an element using array unset syntax and reindex.Signature
The index of the element to remove.
Examples
pop()
Remove and return the last element(s) from the collection.Signature
Number of elements to remove from the end.
TValuewhen$count = 1(single value)ListCollection<TValue>when$count > 1(collection of values)
Examples
Pop single element
Pop multiple elements
shift()
Remove and return the first element(s) from the collection.Signature
Number of elements to remove from the beginning.
TValuewhen$count = 1(single value)ListCollection<TValue>when$count > 1(collection of values)
Examples
Shift single element
Shift multiple elements
Key Points
- All removal methods automatically reindex remaining elements to maintain sequential keys
forget()removes by index without returning the valuepull()removes and returns the value (with optional default)offsetUnset()works with native PHPunset()syntaxpop()removes from the end,shift()removes from the beginning- Both
pop()andshift()can remove multiple elements when count > 1 - Chained removal operations use the reindexed keys after each removal