PHP Export data to CSV file functions/methods

Source: Internet
Author: User

If you are not sure what a CSV file is, see the following article about what files are in CSV format? What is the abbreviation for CSV?

/** * Export Data to CSV file * @paramarray $data data * @paramarray $title _arr title * @paramstring $file _namecsv file name */function export_csv (&    amp; $data, $title _arr, $file _name = ") {Ini_set (" Max_execution_time "," 3600 ");    $csv _data = ";    /** Title */$nums = count ($title _arr); for ($i = 0; $i < $nums-1; + + $i) {$csv _data. = ' ". $title _arr[$i].    ‘",‘; } if ($nums > 0) {$csv _data. = ' ". $title _arr[$nums-1].    """; } foreach ($data as $k + $row) {for ($i = 0; $i < $nums-1; + + $i) {$row [$i] = Str_replace ("            "", "" ", $row [$i]); $csv _data. = ' "'. $row [$i].        ‘",‘; } $csv _data. = ' "'. $row [$nums-1].        """;    Unset ($data [$k]);    } $csv _data = mb_convert_encoding ($csv _data, "cp936", "UTF-8"); $file _name = Empty ($file _name)?    Date (' Y-m-d-h-i-s ', Time ()): $file _name;    if (Strpos ($_server[' http_user_agent '), "MSIE")) {//resolve bug $file _name = UrlEncode ($file _name) for IE output Chinese name garbled $file _name = Str_replacE (' + ', '%20 ', $file _name); } $file _name = $file _name.    '. csv ';    Header ("content-type:text/csv;");    Header ("Content-disposition:attachment;filename=". $file _name);    Header (' cache-control:must-revalidate,post-check=0,pre-check=0 ');    Header (' expires:0 ');    Header (' Pragma:public '); echo $csv _data;}

Application Examples:

$header = Array (' 0 ' = ' parameter id ', ' 1 ' = ' parameter name ', ' 2 ' = ' count ', ' 3 ' = '% ' Count of statistics ', ' 4 ' = ' unique users ', ' 5 ' + ' unique users ' percentage ', ' 6 ' = ' per capita count ');/** list item */foreach ($list as $key + $row) {$csvList [] = Array (' 0 ' = = $row [' Param_key '], ' 1 ' = = $ro w[' Param_name '], ' 2 ' = $row [' nums '], ' 3 ' = ' + ' $row [' percent_nums ']. '% ', ' 4 ' = = $row [' total_users '], ' 5 ' = ' + ' $row [' percent_total_users ']. ' % ', ' 6 ' = = $row [' avg ']);} /** Export CSV */$title = Str_replace (', '-', $title); Export_csv ($csvList, $header, $title);

PHP Export data to CSV file functions/methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.