具有相同ID的CSV文件多行

时间:2016-09-28 15:40:02

标签: php xml csv concatenation

我有一个包含电子商务平台产品信息的CSV文件。 CSV文件包含多个具有相同产品ID和多个描述的行。每个值都由" ^"分隔。而不是逗号。 我想要实现的是将这些多行合并为一个和逗号分隔,并且只有一行包含产品信息。所以我的问题是组合多个具有相同属性的行。

以下是今天文件的格式化

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文件中。

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

谢谢!

1 个答案:

答案 0 :(得分: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";
    }

快乐的代码......

相关问题