erme Posted October 17, 2012 Share Posted October 17, 2012 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)).'",'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/269580-export-product-array-to-csv/ Share on other sites More sharing options...
Barand Posted October 17, 2012 Share Posted October 17, 2012 foreach ($defineProducts as $id => $array) { rtrim_csv_out( "$id" ); rtrim_csv_out( $array['name'] ); rtrim_csv_out( $array['price'] ); rtrim_csv_out( "description test $id" ); echo "\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/269580-export-product-array-to-csv/#findComment-1385756 Share on other sites More sharing options...
erme Posted October 17, 2012 Author Share Posted October 17, 2012 foreach ($defineProducts as $id => $array) { rtrim_csv_out( "$id" ); rtrim_csv_out( $array['name'] ); rtrim_csv_out( $array['price'] ); rtrim_csv_out( "description test $id" ); echo "\n"; } You, sir, are a star! Quote Link to comment https://forums.phpfreaks.com/topic/269580-export-product-array-to-csv/#findComment-1385757 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.