0

it's the first time I try to export a csv with php and I'm having a problem, I'm trying to export all information from an array in CSV form, but when I'm doing the foreach to organize the information that goes to the array, after exporting the information only appears in the csv if I use the "ECHO" during the array, but that bothers me because it consumes more memory. How can I put the information in csv without using "Echo" during foreach ?

my code.

echo "name" . ',' . "lastname"  . ',' . "email" . ',' . "id" . ',' . "create"  . ',' . "group" . ',' . "region"  . ',' . "city"  . ',' . "postcode"  . ',' . "telephone" . "\n";
        $clients = array();
        foreach ($collection as $item) {



            $users = Mage::getModel('customer/customer')->load($item['entity_id']);


            $defaultBillingAddress = Mage::getModel('customer/address')->load($users->getData('default_billing'));


            echo $clients[] = $users->getData('firstname');
            echo $clients[] = ',' . $users->getData('lastname');
            echo $clients[] = ',' . $users->getData('email');
            echo $clients[] = ',' . $users->getData('entity_id');
            echo $clients[] = ',' . $users->getData('created_at');
            echo $clients[] = ',' . $defaultBillingAddress->getData('region');
            echo $clients[] = ',' . $defaultBillingAddress->getData('city');
            echo $clients[] = ',' . $defaultBillingAddress->getData('postcode');
            echo $clients[] = ',' . $defaultBillingAddress->getData('telephone') . "\n";

            break;
        }


        $f = fopen('php://output', 'a');


        foreach ($clients as $clusters) {


            fputcsv($f, $clusters);
        }


        fclose($f);
1322Lucas12123
  • 1
  • 1
  • 1
  • 1
  • You don't need `echo()` in there and frankly it looks wrong. What you probably want to do is "see" the results. For that use `var_dump()` or `print_r()` after you are finished your extractions. ... Also note that each time you do `[]` on the array, a new array index is created to store the value. – GetSet Mar 07 '22 at 03:18
  • But why is the file blank because I don't use echo? – 1322Lucas12123 Mar 07 '22 at 03:19
  • Good point. It's blank because of how you open the output file with this line `$f = fopen('php://output', 'a');`. Where are you trying to export to ultimately? You can still use that line without all the echoes, just echo out the array after it separately. – GetSet Mar 07 '22 at 03:21
  • I think this article you can check it [Export to CSV via PHP](https://stackoverflow.com/questions/4249432/export-to-csv-via-php) – wildgamer Mar 07 '22 at 03:32
  • I think this article you can check it: [Export to CSV via PHP](https://stackoverflow.com/questions/4249432/export-to-csv-via-php) – wildgamer Mar 07 '22 at 03:33
  • Does this answer your question? [Export to CSV via PHP](https://stackoverflow.com/questions/4249432/export-to-csv-via-php) – kmoser Mar 07 '22 at 04:16

1 Answers1

0
$f = fopen('php://output');
foreach ($collection as $item) {
   $row = [
       $users->getData('firstname'),
       ...
       $defaultBillingAddress->getData('telephone'),
   ];
   fputcsv($f, $row);
}
fclose($f);
WinterSilence
  • 413
  • 3
  • 15