{"id":1166,"date":"2023-11-18T00:00:00","date_gmt":"2023-11-18T05:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=1166"},"modified":"2023-11-29T18:45:52","modified_gmt":"2023-11-29T23:45:52","slug":"how-to-create-csv-output-from-php-code","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/how-to-create-csv-output-from-php-code\/","title":{"rendered":"How to create CSV output from PHP code"},"content":{"rendered":"\n<p>This post shows how to generate CSV output from PHP code.<\/p>\n\n\n\n<p><strong>In PHP, the fputcsv() function generates a csv file when it is provided a valid two dimensional array.<\/strong><\/p>\n\n\n\n<p>Following is an example with explanation<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ create a two dimensional array of data\r\n$myarrays = array( array ('Canada', 'China'), array('9.985 million sq km', '9.597 million sq km'));\r\n\r\n\/\/ open a file to write data\r\n$filehandle = fopen('myfile.csv', 'w');\r\n\r\n\/\/ loop through the array\r\nforeach ($myarrays as $arr) {\r\n\r\n    \/\/ write data to csv file \r\n    fputcsv($arr); \r\n}\r\n\r\n\/\/ close the filehandle\r\nfclose($filehandle);<\/pre>\n\n\n\n<p>The content of myfile.csv is:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">'Canada','9.985 million sq km'\r\n'China','9.597 million sq km'<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This post shows how to generate CSV output from PHP code. In PHP, the fputcsv() function generates a csv file when it is provided a valid two dimensional array. Following is an example with explanation \/\/ create a two dimensional array of data $myarrays = array( array (&#8216;Canada&#8217;, &#8216;China&#8217;), array(&#8216;9.985 million sq km&#8217;, &#8216;9.597 million [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1194,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[426,425,424],"class_list":["post-1166","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-data-output-in-php","tag-php-csv-generation","tag-php-programming-tips"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=1166"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1166\/revisions"}],"predecessor-version":[{"id":1167,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1166\/revisions\/1167"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media\/1194"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=1166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=1166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=1166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}