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!