title: My PHP nested loops use way too much memory
body: <p>I'm trying to program a utility that handles a large volume of data and memory is a factor.  Unfortunately each time this set of loops I have runs, it eats apx. 14MB of memory because it is executed thousands of times, even with the unset() calls (and yes I'm aware they do not clean up memory entirely, kind of why I'm asking the question).  I'm wondering if there is an easier way to do this.  Current working code:</p>
  
  <pre><code>        $qr = array();
          foreach($XML-&gt;row as $row)
          {
           $ra = array();
           foreach($row as $key =&gt; $value)
           {
           $ra[$key] = $value[0];
           unset($key,$value);
           }
          $qr[] = $ra;
          unset($row,$ra);
          }
          unset($XML);
          return $qr;
  </code></pre>
  
  <p>Another attempt was to do this, but it lags out.  Anybody know what I'm doing wrong?</p>
  
  <pre><code>        $qr = array();
          while(list(,$row) = each($XML-&gt;row))
          {
           $ra = array();
           while(list($key,$value) = each($row))
           {
           $ra[$key] = $value[0];
           unset($key,$value);
           }
          $qr[] = $ra;
          unset($row,$ra);
          }
          unset($XML);
          return $qr;
  </code></pre>
  
  <p>Basically in the first loop, I'm just trying to do a basic array/object iteration.  In the 2nd loop, I'm trying to go through each array value and get the 1st element while maintaining object/array index association.  It seems I originally wrote it like this due to it being the only thing that worked (because it's looping through a SimpleXML Object).  Any tips on speeding this thing up or figuring out how to make it not eat memory would be appreciated.</p>
  
  <p>I'm looking for solutions for garbage collection or more efficient code.  I do not plan on replacing SimpleXML as there is no need for it.  <strong>More clearly, I'm looking for:</strong></p>
  
  <ul>
  <li>A way to iterate the SimpleXML object without needing to call the inner loop (which is only due to me doing $value[0].  Why is that necessary?</li>
  <li>A way which is more efficient (either speed or memory-wise) for iterating through the data</li>
  </ul>