title: PHP - parse a csv file and write output to another csv file
body: <p>I am working on Magento products csv file and i have a csv file with 3 columns like</p>
  
  <pre><code>&lt;pre&gt;Name, description, Color&lt;/pre&gt;
  &lt;pre&gt;t-shirt, tshirt description, "green, blue, yellow"&lt;/pre&gt;
  </code></pre>
  
  <p>Now based on the values of column 3rd column (color) i want to output each row in a separate file say "new_products.csv" in a manner so the resultant file would be like</p>
  
  <pre><code>&lt;p&gt;Name, description, Color&lt;/p&gt;
  &lt;pre&gt;t-shirt, tshirt description, green&lt;/pre&gt;
  &lt;pre&gt;t-shirt, tshirt description, blue&lt;/pre&gt;
  &lt;pre&gt;t-shirt, tshirt description, yellow&lt;/pre&gt;
  </code></pre>
  
  <p>i have started with this code</p>
  
  <pre><code>$row = 1;
  
  if (($handle = fopen("product.csv", "r")) !== FALSE) {
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
      $num = count($data);
              $name = $data[0];
      $desc = $data[1];
  
      for ($c=0; $c &lt; $num; $c++) {
  
  
          $fp = fopen('new_products.csv', 'w');
  
          if($c == 2){ //if we are at color field
              $color = explode(',', $data[$c]);
              $color_count = count($color);
              for($i=0; $i&lt;$color_count; $i++){
  
                  fputcsv($fp, array($name,$desc,$color[$i]));
              }
  
          }
  
      }
              $row++;
  }
  fclose($handle);
  }
  </code></pre>
  
  <p>However the above code only outputting the last row only.</p>
  
  <p>Thanks</p>