Home  >  Article  >  Backend Development  >  php fputcsv command Small problems encountered when writing csv files (multidimensional array connector)_PHP tutorial

php fputcsv command Small problems encountered when writing csv files (multidimensional array connector)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:29:08943browse

Command: fputcsv()
Command format: int fputcsv ( resource handle [, array fields [, string delimiter [, string enclosure]]] )
Command analysis: fputcsv() format a line (passed in fields array) Convert to CSV format and write to the file specified by handle. Returns the length of the written string, or FALSE on error. The optional delimiter parameter sets the field delimiter (only one character is allowed). The default is comma:,. The optional enclosure parameter sets the field wrapper (only one character allowed). The default is double quotes: ".
Write code (error code):

Copy code The code is as follows:

< ;?php
$users = array(
array("username","department","title");
array("user1","1","Secretariat","Department member");
array("user2","2","office","section member");
array("user3","3","Logistics Office","section member") ;
);
$handle = fopen("html/csvfile.csv","w");
foreach($users as $line){
fputcsv($user,$line) ;
}
//When you "invite" the file, remember to "send it back"
fclose($handle);
?>

Code Analysis: Create an empty csvfile.csv file in the html/file directory (create it manually), then open it with the command and write the files of the users array:

php fputcsv command Small problems encountered when writing csv files (multidimensional array connector)_PHP tutorial Seeing this, everyone must know what is wrong. ";" is the culprit. I suddenly realize that the connector in the multi-dimensional array is ",". I mistakenly typed a semicolon. This error is very hidden. Don't make it again. !
Remember: Multidimensional arrays are connected using the

PHP fputcsv() function

http://www.bkjia.com/PHPjc/323491.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323491.htmlTechArticleCommand: fputcsv() Command format: int fputcsv ( resource handle [, array fields [, string delimiter [, string enclosure]]] ) Command analysis: fputcsv() will pass a row (passed in fields array...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn