title: How to unset (remove) a collection element after fetching it?
body: <p>I have a collection which I want to iterate and modify while I fetch some of its elements. But I could't find a way or method to remove that fetched element. </p>
  
  <pre><code>$selected = []; 
  foreach ($collection as $key =&gt; $value) {
        if ($collection-&gt;selected == true) {
            $selected[] = $value;
            unset($value);
        }
  }
  </code></pre>
  
  <p>This is just a representation of my question for demonstration.</p>
  
  <p>After @Ohgodwhy advice the forget() function I checked it again and saw that I actually misunderstood the function. It was exactly as I was looking for. </p>
  
  <p>So for working solution I have added <code>$collection-&gt;forget($key)</code> inside the if statement.</p>
  
  <p><strong>Below is the working solution of my problem, using @Ohgodwhy's solution:</strong></p>
  
  <pre><code>$selected = []; 
  foreach ($collection as $key =&gt; $value) {
        if ($collection-&gt;selected == true) {
            $selected[] = $value;
            $collection-&gt;forget($key);
        }
  }
  </code></pre>
  
  <p>(this is just a demonstration)</p>