You are viewing an old revision of this post, from January 2, 2018 @ 18:19:28. See below for differences between this version and the current revision.

PHP – Add data to CSV file in specific columns

Base on the requirement of my client, I have to create a CSV file which has a lot of columns, and hundreds of rows. The header looks like this: SKU | category | description | image | small_image | thumbnail | price | color I need to read the data from other files and put the suitable data in the correct column. I tried many different ways but it didn't work. Finally, I found out a way to do it quickly, here is how I did with PHP: At first, I break out the columns into an array precisely as they appear in the raw CSV file:
$columns = ["sku","category","description","image","small_image","thumbnail", "price","color"];
Then I open the file and iterate over it, building an associative array of key-value pairs, checking whether or not a file exists for that image name, and if so, assigning the correct name to the array.
$rootDir = ""; //Root directory where the image files are located
$file = fopen("filehandle.csv", "r"); //Open the old file for reading
$newFile = fopen("newfilehandle.csv", "w"); //Create a new file for writing

while (($data = fgetcsv($file)) !== FALSE) {
    $row = array_combine($columns, $data);
    $filename = "{$row['sku']}.jpeg";
    if (file_exists("$rootDir/$filename")) {
        $row['image'] = $filename;
        $row['small_image'] = $filename;
        $row['thumbnail'] =  $filename; 
    }
    fputcsv($newFile, array_values($row)); //Write data into new file
}

fclose($file);
fclose($newFile);
Hope it is useful to you 🙂

Revisions

Revision Differences

January 2, 2018 @ 18:19:28Current Revision
Title
Deleted: PHP - Add data to SCV file in specific columns Added: PHP - Add data to CSV file in specific columns
Content
Deleted: I have a CSV file which has a lot of columns, and hundreds of rows. Added: Base on the requirement of my client, I have to create a CSV file which has a lot of columns, and hundreds of rows.
Unchanged: The header looks like this:Unchanged: The header looks like this:
Unchanged: SKU | category | description | image | small_image | thumbnail | price | colorUnchanged: SKU | category | description | image | small_image | thumbnail | price | color
Deleted: I want to read the data from other files and put the suitable data in the correct column. Added: I need to read the data from other files and put the suitable data in the correct column.
Deleted: I have tried many times and many different ways. Finally, I found out a way to do it quickly, here is how I did with PHP: Added: I tried many different ways but it didn't work. Finally, I found out a way to do it quickly, here is how I did with PHP:
Deleted: At first, I break out the columns into an array precisely as they appear in your raw CSV file: Added: At first, I break out the columns into an array precisely as they appear in the raw CSV file:
Unchanged: <pre class="lang-php prettyprint prettyprinted" ><code><span class="pln">$columns </span><span class="pun">=</span> <span class="pun">[</span><span class="str">" sku"</span><span class="pun">,</span><span class="str">" category"</span><span class="pun">,</span><span class="str">" description"</span><span class="pun">,</span><span class="str">" image"</span><span class="pun">,</span><span class="str">" small_image"</span><span class="pun">,</span><span class="str">" thumbnail"</span><span class="pun">,</span> <span class="str">" price"</span><span class="pun">,</span><span class="str">" color"</span><span class="pun">]; </span></code></pre>Unchanged: <pre class="lang-php prettyprint prettyprinted" ><code><span class="pln">$columns </span><span class="pun">=</span> <span class="pun">[</span><span class="str">" sku"</span><span class="pun">,</span><span class="str">" category"</span><span class="pun">,</span><span class="str">" description"</span><span class="pun">,</span><span class="str">" image"</span><span class="pun">,</span><span class="str">" small_image"</span><span class="pun">,</span><span class="str">" thumbnail"</span><span class="pun">,</span> <span class="str">" price"</span><span class="pun">,</span><span class="str">" color"</span><span class="pun">]; </span></code></pre>
Unchanged: Then I open the file and iterate over it, building an associative array of key-value pairs, checking whether or not a file exists for that image name, and if so, assigning the correct name to the array.Unchanged: Then I open the file and iterate over it, building an associative array of key-value pairs, checking whether or not a file exists for that image name, and if so, assigning the correct name to the array.
Unchanged: <pre class="lang-php prettyprint prettyprinted" ><code><span class="pln">$rootDir </span><span class="pun">=</span> <span class="str">" "</span><span class="pun">;</span> <span class="com">//Root directory where the image files are located</span><span class="pln">Unchanged: <pre class="lang-php prettyprint prettyprinted" ><code><span class="pln">$rootDir </span><span class="pun">=</span> <span class="str">" "</span><span class="pun">;</span> <span class="com">//Root directory where the image files are located</span><span class="pln">
Unchanged: $file </span><span class="pun">= </span><span class="pln"> fopen</span><span class="pun">( </span><span class="str">" filehandle.csv" </span><span class="pun">,</span> <span class="str">" r"</span><span class="pun">);</span> <span class="com">//Open the old file for reading</span><span class="pln">Unchanged: $file </span><span class="pun">= </span><span class="pln"> fopen</span><span class="pun">( </span><span class="str">" filehandle.csv" </span><span class="pun">,</span> <span class="str">" r"</span><span class="pun">);</span> <span class="com">//Open the old file for reading</span><span class="pln">
Unchanged: $newFile </span><span class="pun">= </span><span class="pln"> fopen</span><span class="pun">( </span><span class="str">" newfilehandle.csv" </span><span class="pun">,</span> <span class="str">" w"</span><span class="pun">);</span> <span class="com">//Create a new file for writing</span>Unchanged: $newFile </span><span class="pun">= </span><span class="pln"> fopen</span><span class="pun">( </span><span class="str">" newfilehandle.csv" </span><span class="pun">,</span> <span class="str">" w"</span><span class="pun">);</span> <span class="com">//Create a new file for writing</span>
Unchanged: <span class="kwd">while</span> <span class="pun">( (</span><span class="pln">$data </span><span class="pun">= </span><span class="pln"> fgetcsv</span><span class="pun">( </span><span class="pln">$file</span><span class="pun">))</span> <span class="pun">!= =</span><span class="pln"> FALSE</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">Unchanged: <span class="kwd">while</span> <span class="pun">( (</span><span class="pln">$data </span><span class="pun">= </span><span class="pln"> fgetcsv</span><span class="pun">( </span><span class="pln">$file</span><span class="pun">))</span> <span class="pun">!= =</span><span class="pln"> FALSE</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
Unchanged: $row </span><span class="pun">= </span><span class="pln"> array_combine</span><span class="pun">( </span><span class="pln">$columns</span><span class="pun">,</span><span class="pln"> $data</span><span class="pun">) ;</span><span class="pln">Unchanged: $row </span><span class="pun">= </span><span class="pln"> array_combine</span><span class="pun">( </span><span class="pln">$columns</span><span class="pun">,</span><span class="pln"> $data</span><span class="pun">) ;</span><span class="pln">
Unchanged: $filename </span><span class="pun">=</span> <span class="str">" {$row['sku']}.jpeg" </span><span class="pun">;</span>Unchanged: $filename </span><span class="pun">=</span> <span class="str">" {$row['sku']}.jpeg" </span><span class="pun">;</span>
Unchanged: <span class="kwd">if</span> <span class="pun">( </span><span class="pln">file_ exists</span><span class="pun">( </span><span class="str">" $rootDir/$filename" </span><span class="pun">))</span> <span class="pun">{</span><span class="pln">Unchanged: <span class="kwd">if</span> <span class="pun">( </span><span class="pln">file_ exists</span><span class="pun">( </span><span class="str">" $rootDir/$filename" </span><span class="pun">))</span> <span class="pun">{</span><span class="pln">
Unchanged: $row</span><span class="pun">[</span><span class="str">'image'</span><span class="pun">]</span> <span class="pun">= </span><span class="pln"> $filename</span><span class="pun">; </span><span class="pln">Unchanged: $row</span><span class="pun">[</span><span class="str">'image'</span><span class="pun">]</span> <span class="pun">= </span><span class="pln"> $filename</span><span class="pun">; </span><span class="pln">
Unchanged: $row</span><span class="pun">[</span><span class="str">'small_ image'</span><span class="pun">]</span> <span class="pun">= </span><span class="pln"> $filename</span><span class="pun">; </span><span class="pln">Unchanged: $row</span><span class="pun">[</span><span class="str">'small_ image'</span><span class="pun">]</span> <span class="pun">= </span><span class="pln"> $filename</span><span class="pun">; </span><span class="pln">
Unchanged: $row</span><span class="pun">[</span><span class="str">'thumbnail'</span><span class="pun">]</span> <span class="pun">= </span><span class="pln"> $filename</span><span class="pun">;</span> Unchanged: $row</span><span class="pun">[</span><span class="str">'thumbnail'</span><span class="pun">]</span> <span class="pun">= </span><span class="pln"> $filename</span><span class="pun">;</span>
Unchanged: <span class="pun">}</span><span class="pln">Unchanged: <span class="pun">}</span><span class="pln">
Deleted: fputcsv</span><span class="pun">( </span><span class="pln">$newFile</span><span class="pun">,</span><span class="pln"> array_values</span><span class="pun">( </span><span class="pln">$row</span><span class="pun">));</span> <span class="com">//write data into new file</span> Added: fputcsv</span><span class="pun">( </span><span class="pln">$newFile</span><span class="pun">,</span><span class="pln"> array_values</span><span class="pun">( </span><span class="pln">$row</span><span class="pun">));</span> <span class="com">//Write data into new file</span>
Unchanged: <span class="pun">}</span><span class="pln">Unchanged: <span class="pun">}</span><span class="pln">
Unchanged: fclose</span><span class="pun">( </span><span class="pln">$file</span><span class="pun">) ;</span><span class="pln">Unchanged: fclose</span><span class="pun">( </span><span class="pln">$file</span><span class="pun">) ;</span><span class="pln">
Deleted: fclose</span><span class="pun">( </span><span class="pln">$newFile</span><span class="pun">); Added: fclose</span><span class="pun">( </span><span class="pln">$newFile</span><span class="pun">) ;</span></code></pre>
Deleted: </span></code> Hope it is useful for you 🙂</pre> Added: Hope it is useful to you 🙂
Deleted: &nbsp; 

Note: Spaces may be added to comparison text to allow better line wrapping.

No comments yet.

Leave a Reply