2016-09-28 94 views
-1

我已经获得了包含电子商务平台产品信息的CSV文件。 CSV文件包含具有相同产品ID和多个说明的多行。每个值由“^”而不是逗号分隔。 我想要实现的是将这些多行合并为一个逗号分隔,并且只有一行产品信息。所以我的问题是组合多个具有相同属性的行。具有相同ID的CSV文件多行代码

这是今天

productID^Element^Value<br> 
id01^Status^01<br> 
id01^edited^2016-01-01<br> 
id01^Longdesc^Here goes the description<br> 
id01^Longdesc^Here is the second line of description<br> 
id01^longdesc^And the third row<br> 
id01^image^Link to image 1<br> 

文件的格式化这就是我要找的实现:

ID01, 2016-01-01, Here goes the description Here is the second line of description And the third row, link to image 

另一个想法是把结构的很好的XML文件。

希望你们有这方面的一些很好的解决方案,可以帮助我。

谢谢!

+0

这似乎是一个很简单的问题 - 遍历线,解析它们,并重新打印出来你最喜欢的方式。你到目前为止尝试了什么? –

+0

我试过这个:http://stackoverflow.com/questions/25610886/how-to-concatenate-values-from-multiple-rows-with-the-same-id-to-a-comma-separat但是不能得到长话结合。 – Koolander

回答

0

请尝试下列代码。我相信这段代码可以正常工作。

public function import_product_from_csv() { 
     ini_set('max_execution_time', 1000); 
     $fh = fopen('your_csv_file.csv', 'r'); 
     fgets($fh, 4096); 

     while (!feof($fh)) { 
      $buffer = fgets($fh, 4096); 
      $arrTemp = explode('^',$buffer); 

      if($arrTemp[0] != "") { 
       $data = array(
        'productID' => $arrTemp[0], // codice provincia 
        'Element' => $arrTemp[1], //codice comune 
        'Value' => $arrTemp[2] //denominazione 
       ); 
       $sql_query = "insert into table_name (productID, Element, Value) values ('".$arrTemp[0]."', '".$arrTemp[1]."', '".$arrTemp[2]."')"; 
       mysql_query($sql_query); 
      } 

      /*echo "<pre>"; 
      print_r($arrTemp); 
      echo "</pre>";*/ 
     } 
     fclose($fh); 
     echo "Uploaded"; 
    } 

快乐码...

+0

Thx to @sebastianbrosch得到它的工作 – Koolander

+0

也thx @razibalmamun – Koolander

+0

如果你的问题解决了,请接受这个答案 –