Jump to content

Export Product Array To Csv


erme

Recommended Posts

Basically trying to export a list of products in an array to a comma separated csv. I;ve managed to get it working but would like to know how to make use of the array ID's. Below is what I have

 

<?php
//The list of products
$defineProducts[801] = array(name=>'Product 1', price=>'175.95');
$defineProducts[802] = array(name=>'Product 2', price=>'235.95');
$defineProducts[803] = array(name=>'Product 3', price=>'245.95');
$defineProducts[804] = array(name=>'Product 4', price=>'215.95');
$defineProducts[805] = array(name=>'Product 5', price=>'230.95');

   $filename = date("Ymd")."_csvfile.csv";
   header("Content-type: application/csv");
   header("Content-Disposition: attachment; filename=$filename");
   $field_arr = array('productid','name','price','description');
   foreach($field_arr as $val)
    rtrim_csv_out($val);
   echo "\n";
   for($i=1; $i<10; $i++)
   {
    rtrim_csv_out( "$i" );
    rtrim_csv_out( $defineProducts[801]['name'] ); //currently only exporting product 801
    rtrim_csv_out( $defineProducts[801]['price'] );
    rtrim_csv_out( "description test $i" );
    echo "\n";
   }
   function rtrim_csv_out($str)
   {
    echo '"'.rtrim(str_replace('"','""',$str)).'",';
   }
?>

Link to comment
https://forums.phpfreaks.com/topic/269580-export-product-array-to-csv/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.