title: How to loop a Magento collection?
body: <p><strong>Basically, I need to get a CSV file of my customers, generated automatically in a script every day.</strong>
  I've tried several ways, but they are too slow or actually exhausted of memory.</p>
  
  <p>*<em>1) foreach through collection resource *</em></p>
  
  <pre><code>$collection = Mage::getResourceModel('customer/customer_collection')
  -&gt;addAttributeToSelect('email')
  -&gt;addAttributeToSelect('created_at')
  -&gt;joinAttribute('billing_company', 'customer_address/company', 'default_billing', null, 'left')
  -&gt;joinAttribute('billing_street', 'customer_address/street', 'default_billing', null, 'left')
  -&gt;joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
  -&gt;joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
  -&gt;joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
  -&gt;joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
  -&gt;joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
  
  foreach($collection as $customer) {
  echo $customer-&gt;getFirstname() . ",";
  }
  </code></pre>
  
  <p>2) <strong>foreach and load customer</strong></p>
  
  <pre><code>$collection = Mage::getResourceModel('customer/customer_collection');
  foreach($collection as $customer) {
    $fullcustomer = Mage::getModel("customer/customer")-&gt;load($customer-&gt;getId());
    echo $fullcustomer-&gt;getFirstname() . ",";
  }
  </code></pre>
  
  <p>Any ideas? </p>
  
  <p>Thanks!</p>