2014-10-09 95 views
1

我没有要求导入来自客户的订单,这将含有作为他们的客户数据,即第一行约4字段CSV解析器,用于在一个csv文件报头和细节上的线

customer number, order date, order no, delivery date 

下一个X行数将包含需要product codequantity的两个字段。

我有其他程序,将拉入标准的价格清单等,并解析,但我很努力得到我的头如何将CSV文件拆分成两个不同的部分。

如何可以做到这一点任何帮助或想法可以理解

+0

如果它只是在第一行,为什么不跳呢?它不是CSV的真正组成部分,只是其元数据。通过跳过它,我的意思是将该行作为单独的进程来读取。 – Anthony 2014-10-09 07:11:08

+0

嗨安东尼,对不起,我应该说,第一行将包含实际的标题数据,而不是列标题,其余的行将包含行项目。所以没有标题等,因为非常少量的字段只有两个单独的类型;) – 2014-10-09 07:18:34

+0

@ Anthony的建议仍然成立,您可以分别读取第一行的数据并阅读其余的内容,然后在内部一起使用它们。 – 2014-10-09 07:23:10

回答

3

我会建议使用此库,其将通过name类似于SQL结果数组

customer number, order date, order no, delivery date 

给你列解析它在一个你想要的方式https://github.com/parsecsv/parsecsv-for-php

例如

require_once 'parsecsv.lib.php'; 
$csv = new parseCSV('data.csv'); 
print_r($csv->data); 
+0

嗨,我看到库中有一个偏移量和限制参数。所以我将能够对文件右侧运行两个查询,1为标题行,然后偏移细节行? – 2014-10-09 07:24:56

+1

我认为它确实在特征中看到[github repo](https://github.com/parsecsv/parsecsv-for-php):“用于过滤数据的基本类SQL条件,偏移和限制选项”。 – Saqueib 2014-10-09 07:27:20

2

使用parsecsv library你可以这样做:

//get customer data first 
// limit the number of returned rows. 
$csv->limit = 1; 
// Parse 'order.csv' using automatic delimiter detection. 
$csv->auto('order.csv'); 
//retrieve the customer data 
$customer_data = $csv->data; 

//get order details after 
//skip the first line 
$csv->offset = 1; 
// Parse 'order.csv' using automatic delimiter detection. 
$csv->auto('order.csv'); 
$order_details = $csv->data; 
+1

哦,伙计,我希望我早些时候在我的生活中找到了这个图书馆,非常感谢你为大家提供的帮助。不知道谁为正确的答案而感谢,因为所有的帮助,但我会说Saqueib建议这个图书馆 – 2014-10-09 08:04:38