Generate a csv file using the phparray array

Source: Internet
Author: User
The method for generating csv files in php is very simple. here I will introduce an instance I used to convert the array directly into a csv file for output. For more information, see, the code is as follows :? Php $ data... the method for generating csv files in php is very simple. here I will introduce an instance I used to convert the array directly into a csv file for output. For more information, see, the code is as follows:

 


Function outputCSV ($ data) {$ outputBuffer = fopen ("php: // output", 'w'); foreach ($ data as $ val) {foreach ($ val as $ key => $ val2) {$ val [$ key] = iconv ('utf-8', 'gbk', $ val2 ); // CSV Excel supports GBK encoding and must be converted; otherwise, garbled code} fputcsv ($ outputBuffer, $ val);} fclose ($ outputBuffer );}

Example 2: Read a csv file instance, read cvs, read a row from the fgetcsv () file pointer, and parse the CSV field. for example, to read the following csv file, the code is as follows:

  $ Num fields in line $ row:
\ N "; $ row ++; for ($ c = 0; $ c <$ num; $ c ++) {// pay attention to Chinese garbled characters $ data [$ c] = iconv ("gbk", "UTF-8 // IGNORE", $ data [$ c]); echo $ data [$ c]."
\ N ";}} fclose ($ handle );


Article address:

Reprint at will ^ please include the address of this article!

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.