<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Write a field array as a CSV line</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="splfileobject.fpassthru.html">SplFileObject::fpassthru</a></div> <div class="next" style="text-align: right; float: right;"><a href="splfileobject.fscanf.html">SplFileObject::fscanf</a></div> <div class="up"><a href="class.splfileobject.html">SplFileObject</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="splfileobject.fputcsv" class="refentry"> <div class="refnamediv"> <h1 class="refname">SplFileObject::fputcsv</h1> <p class="verinfo">(PHP 5 >= 5.4.0)</p><p class="refpurpose"><span class="refname">SplFileObject::fputcsv</span> — <span class="dc-title">Write a field array as a CSV line</span></p> </div> <div class="refsect1 description" id="refsect1-splfileobject.fputcsv-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">int</span> <span class="methodname"><strong>SplFileObject::fputcsv</strong></span> ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$fields</code></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$delimiter</code><span class="initializer"> = ','</span></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$enclosure</code><span class="initializer"> = '"'</span></span> ]] )</div> <p class="para rdfs-comment"> Writes the <em><code class="parameter">fields</code></em> array to the file as a CSV line. </p> </div> <div class="refsect1 parameters" id="refsect1-splfileobject.fputcsv-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <span class="term"><em><code class="parameter">fields</code></em></span> <dd> <p class="para"> An array of values. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">delimiter</code></em></span> <dd> <p class="para"> The optional <em><code class="parameter">delimiter</code></em> parameter sets the field delimiter (one character only). </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">enclosure</code></em></span> <dd> <p class="para"> The optional <em><code class="parameter">enclosure</code></em> parameter sets the field enclosure (one character only). </p> </dd> </dt> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-splfileobject.fputcsv-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the length of the written string or <strong><code>FALSE</code></strong> on failure. </p> <p class="para"> Returns <strong><code>FALSE</code></strong>, and does not write the CSV line to the file, if the <em><code class="parameter">delimiter</code></em> or <em><code class="parameter">enclosure</code></em> parameter is not a single character. </p> </div> <div class="refsect1 errors" id="refsect1-splfileobject.fputcsv-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> An <strong><code>E_WARNING</code></strong> level error is issued if the <em><code class="parameter">delimiter</code></em> or <em><code class="parameter">enclosure</code></em> parameter is not a single character. </p> </div> <div class="refsect1 examples" id="refsect1-splfileobject.fputcsv-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="splfileobject.fputcsv.examples.basic"> <p><strong>Example #1 <span class="methodname"><strong>SplFileObject::fputcsv()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$list </span><span style="color: #007700">= array (<br /> array(</span><span style="color: #DD0000">'aaa'</span><span style="color: #007700">, </span><span style="color: #DD0000">'bbb'</span><span style="color: #007700">, </span><span style="color: #DD0000">'ccc'</span><span style="color: #007700">, </span><span style="color: #DD0000">'dddd'</span><span style="color: #007700">),<br /> array(</span><span style="color: #DD0000">'123'</span><span style="color: #007700">, </span><span style="color: #DD0000">'456'</span><span style="color: #007700">, </span><span style="color: #DD0000">'789'</span><span style="color: #007700">),<br /> array(</span><span style="color: #DD0000">'"aaa"'</span><span style="color: #007700">, </span><span style="color: #DD0000">'"bbb"'</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #0000BB">$file </span><span style="color: #007700">= new </span><span style="color: #0000BB">SplFileObject</span><span style="color: #007700">(</span><span style="color: #DD0000">'file.csv'</span><span style="color: #007700">, </span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /><br />foreach (</span><span style="color: #0000BB">$list </span><span style="color: #007700">as </span><span style="color: #0000BB">$fields</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$file</span><span style="color: #007700">-></span><span style="color: #0000BB">fputcsv</span><span style="color: #007700">(</span><span style="color: #0000BB">$fields</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will write the following to <em>file.csv</em>:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> aaa,bbb,ccc,dddd 123,456,789 """aaa""","""bbb""" </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-splfileobject.fputcsv-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.fputcsv.html" class="function" rel="rdfs-seeAlso">fputcsv()</a> - Format line as CSV and write to file pointer</span></li> <li class="member"> <span class="methodname"><a href="splfileobject.fgetcsv.html" class="methodname" rel="rdfs-seeAlso">SplFileObject::fgetcsv()</a> - Gets line from file and parse as CSV fields</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="splfileobject.fpassthru.html">SplFileObject::fpassthru</a></div> <div class="next" style="text-align: right; float: right;"><a href="splfileobject.fscanf.html">SplFileObject::fscanf</a></div> <div class="up"><a href="class.splfileobject.html">SplFileObject</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>