How to unset (remove) a collection element after fetching it?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Efficiently Unsetting Fetched Collection Elements in PHP
Body:
In an application where you have to iterate through a collection for selection purposes while also removing the selected elements, things can get tricky. Sometimes, you might realize that you need to remove the element after it has already been fetched and stored. This task can seem daunting but is not impossible with appropriate PHP knowledge and Laravel-related framework understanding.
In this blog post, we will discuss how to achieve this goal using a combination of iterating through collections, fetching selected elements, and removing them from the original collection. Firstly, it's crucial to identify suitable approaches for handling these tasks in Laravel. Then, we will examine the code and best practices for each method, along with a step-by-step guide on how you can implement them in your application.
The Approach: Iterate Through Collections and Unset Elements
Let's start by exploring the given code snippet as an example of how to iterate through collections and fetch elements for selection while concurrently removing them from their original collection.
$selected = [];
foreach ($collection as $key => $value) {
if ($collection->selected == true) {
$selected[] = $value;
unset($value);
}
}
This code iterates through the collection, checking for each element whether it's selected or not. If an element is indeed selected, we add it to our `$selected` array and call the `unset()` function on the value being fetched. However, this solution leads us to a couple of problems:
1) The usage of unset() within the loop affects referencing the collection elements in our script (since PHP handles memory management by reference). This can lead to unexpected behavior and errors.
2) Unset() does not remove an element from the collection itself, only affecting its local variable value. In essence, you're temporarily resetting the value of that variable without actually removing it from the collection.
A more thorough solution is required. Thankfully, Laravel offers a better way to handle this situation with their `Collection` class. This collection allows for a cleaner and more efficient approach to addressing our problem.
The Solution: Using Collection's forget() Method
The Laravel Collection class comes equipped with the `forget()` function, which effectively removes an element from the collection by its key. This is particularly useful when iterating through a collection and selecting elements for removal. Here is how you can implement this feature:
$selected = [];
foreach ($collection as $key => $value) {
if ($collection->selected == true) {
$selected[] = $value;
$collection->forget($key);
}
}
By using `forget()`, you remove the element not only from your local variable but also from the collection's internal memory, resulting in a clean and efficient solution. This approach ensures that your code is both accurate and effective without any adverse effects on memory management or other elements of your application.
Conclusion
To adequately handle iterating through collections while removing selected elements efficiently, the Laravel Collection class's `forget()` function provides a more suitable alternative to using `unset()`. In this blog post, we have discussed how this method can be applied effectively in your PHP code for better application performance. Always remember that Laravel offers various helpful tools and techniques to assist you in creating robust and well-performing applications. By utilizing these functions properly, you can create high-quality software with enhanced efficiency.