2016-09-28 51 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

如果你的問題解決了,請接受這個答案 –

相關問題