title: Multiple foreach loop and just another memory leak story
body: <p>To whom may concern,</p>
  
  <p>As we are already known <code>foreach</code> is a painful and a headache while we using it to deal with thoudsands or millions of record. And it will become a bloody killer machine if we have a twin or may be more <code>foreach</code> in a row :)</p>
  
  <p>For example,</p>
  
  <pre><code>foreach ($parent as $parentData) {
      // few conditional added here
      $parentObj = $this-&gt;extract();
      foreach ($chilren as $childrenData) {
          if ($childrenData['id'] === $parentData['id']) {
              $childrenObj = $this-&gt;extract();
              $parentObj-&gt;setData($childrenObj);
              // and even more evil things come here....
          }
      }
      $parentObj-&gt;save();
  }
  </code></pre>
  
  <p>In my situation, I have twin <code>foreach</code>. And each one contains approximate 50,000 ~ 70,000 records. <code>$parent</code> and <code>$children</code> are parameters passed to the method.</p>
  
  <p>The raw data source both of <code>$parent</code> and <code>$children</code> are CSV files. And I'm using <code>yield</code> to transform them to be travelsable with <code>foreach</code>.</p>
  
  <p>There is no trouble with <code>yield</code>, belive me. It's guaranteed by a 60k+ player of this site :)</p>
  
  <p>If you concern about the code: <a href="https://stackoverflow.com/a/37342184/2932590">https://stackoverflow.com/a/37342184/2932590</a></p>
  
  <p>Back to my concernation, I tried to <code>unset</code> both of <code>$parentObj</code> and <code>$childrenObj</code> at the end of first <code>foreach</code>, but unfortunately it's not working. I also tried to using the references <code>&amp;$parentData</code>, but the result is same.</p>
  
  <p>How could I make this work till the rest of my life?</p>
  
  <p>Thanks in Advanced.</p>
  
  <h1>UPDATED</h1>
  
  <p>I got few advised to use SPL Iterators in this case. Can anyone please explain me how it work also?</p>
  
  <p>Thanks.</p>
  
  <h1>UPDATED #2</h1>
  
  <p>I'm using SPL Iterator, below is the new code:</p>
  
  <pre><code>$parent = new IteratorIterator(new ArrayIterator($parentArr));
  $children = new IteratorIterator(new ArrayIterator($chilrenArr));
  foreach ($parent as $index =&gt; $parentData) {
      $parentObj = null;
      // few conditional added here
      $parentObj = $this-&gt;extract();
      $childrenObj = null;
      foreach ($chilren as $key =&gt; $childrenData) {
          if ($childrenData['id'] === $parentData['id']) {
              $childrenObj = $this-&gt;extract();
              $parentObj-&gt;setData($childrenObj);
              // and even more evil things come here....
          }
      }
      $parentObj-&gt;save();
      $childrenObj-&gt;save();
  }
  </code></pre>