{"id":1573,"date":"2016-03-20T05:24:13","date_gmt":"2016-03-20T05:24:13","guid":{"rendered":"http:\/\/www.phpmind.com\/blog\/?p=1573"},"modified":"2016-03-20T05:24:14","modified_gmt":"2016-03-20T05:24:14","slug":"export-php-array-to-csv-download-csv-file","status":"publish","type":"post","link":"https:\/\/www.phpmind.com\/blog\/2016\/03\/export-php-array-to-csv-download-csv-file\/","title":{"rendered":"Export php array to CSV  &#8211; Download CSV File"},"content":{"rendered":"<p>Export php array to CSV<br \/>\nCSV (comma-separated values) is the most widely supported format for transferring tabular data. Exporting data in CSV format is very common in web applications. This simple example will help you get started with CSV and PHP. The main goal is to create a simple way to export data from your website or web application into a CSV that can be downloaded by the user.<\/p>\n<pre>\r\n\r\n    $data = array(\r\n        array('name' => 'A', 'mail' => 'a@gmail.com', 'age' => 43),\r\n        array('name' => 'C', 'mail' => 'c@gmail.com', 'age' => 24),\r\n        array('name' => 'B', 'mail' => 'b@gmail.com', 'age' => 35),\r\n        array('name' => 'G', 'mail' => 'f@gmail.com', 'age' => 22),\r\n        array('name' => 'F', 'mail' => 'd@gmail.com', 'age' => 52),\r\n        array('name' => 'D', 'mail' => 'g@gmail.com', 'age' => 32),\r\n        array('name' => 'E', 'mail' => 'e@gmail.com', 'age' => 34),\r\n        array('name' => 'K', 'mail' => 'j@gmail.com', 'age' => 18),\r\n        array('name' => 'L', 'mail' => 'h@gmail.com', 'age' => 25),\r\n        array('name' => 'H', 'mail' => 'i@gmail.com', 'age' => 28),\r\n        array('name' => 'J', 'mail' => 'j@gmail.com', 'age' => 53),\r\n        array('name' => 'I', 'mail' => 'l@gmail.com', 'age' => 26),\r\n    );\r\n\r\n$fileName_1 = 'Manifest.csv';\r\n        header(\"Cache-Control: must-revalidate, post-check=0, pre-check=0\");\r\n        header('Content-Description: File Transfer');\r\n        header(\"Content-type: text\/csv\");\r\n        header(\"Content-Disposition: attachment; filename={$fileName_1}\");\r\n        header(\"Expires: 0\");\r\n        header(\"Pragma: public\");\r\n        $fh1 = @fopen( 'php:\/\/output', 'w' );\r\n        $headerDisplayed1 = false;\r\n\r\n        foreach ( $data as $data1 ) {\r\n            \/\/ Add a header row if it hasn't been added yet\r\n            if ( !$headerDisplayed1 ) {\r\n                \/\/ Use the keys from $data as the titles\r\n                fputcsv($fh1, array_keys($data1));\r\n                $headerDisplayed1 = true;\r\n            }\r\n\r\n            \/\/ Put the data into the stream\r\n            fputcsv($fh1, $data1);\r\n        }\r\n    \/\/ Close the file\r\n        fclose($fh1);\r\n    \/\/ Make sure nothing else is sent, our file is done\r\n        exit;\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Export php array to CSV CSV (comma-separated values) is the most widely supported format for transferring tabular data. Exporting data in CSV format is very common in web applications. This simple example will help you get started with CSV and PHP. The main goal is to create a simple way to export data from your [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1573","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/comments?post=1573"}],"version-history":[{"count":1,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1573\/revisions"}],"predecessor-version":[{"id":1574,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1573\/revisions\/1574"}],"wp:attachment":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/media?parent=1573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/categories?post=1573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/tags?post=1573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}