具有多个标题行的 CSV;使用php导入mysql

发布于 2024-09-10 17:29:00 字数 1381 浏览 1 评论 0原文

我有一个 CSV 文件,其中包含两组数据 [它是温度记录仪的数据]。我想知道是否有办法将前两行导入为一组数据,将其余行导入为第二组数据。我熟悉 PHP,但从未做过这样的事情。我在下面附上了一部分数据。任何帮助或指导将不胜感激。谢谢。

Ajay

Serial Number,Model,Name,Number,Time of Reading,Sample Rate,Start Time
910000000ebeaa4,DS1922T,SRBA04-240909  ,beaa-TNTVLPRP54,9/30/09 16:52,1,9/24/09 18:59
Sample #,Date/Time,Temp ¡F,Temp ¡C,,,
1,"Thu, Sep, 24, 06:59, PM",82.4,28,,,
2,"Thu, Sep, 24, 07:00, PM",83.3,28.5,,,
3,"Thu, Sep, 24, 07:01, PM",83.3,28.5,,,
4,"Thu, Sep, 24, 07:02, PM",83.3,28.5,,,
5,"Thu, Sep, 24, 07:03, PM",83.3,28.5,,,
6,"Thu, Sep, 24, 07:04, PM",83.3,28.5,,,
7,"Thu, Sep, 24, 07:05, PM",83.3,28.5,,,
8,"Thu, Sep, 24, 07:06, PM",83.3,28.5,,,
9,"Thu, Sep, 24, 07:07, PM",83.3,28.5,,,
10,"Thu, Sep, 24, 07:08, PM",83.3,28.5,,,

解决方案,基于 deceze 下面的评论

,谢谢。这有效[就像sql插入一样]。非常感谢。

<?php
$fhandle = fopen('1.csv', 'r');

$device_header = fgetcsv($fhandle, 1000, ',');                      
$device_data = fgetcsv($fhandle,1000,',');                          

$device = array_combine($device_header, $device_data);          

$data_header = fgetcsv($fhandle,1000,',');                          

$dataset = array();                                             
    while ($data = fgetcsv($fhandle,1000,',')) {  
        $dataset[] = array_combine($data_header, $data);        
}

fclose($fhandle);

?>

I've got a CSV file with two sets of data in it [its pulled of a temperature logger]. I'm wondering if there's a way to import the first two rows as one set of data and the rest as a second set of data. I'm familiar with PHP, but have never had to do anything like this. I've attached a subset of data below. Any help or direction would be greatly appreciated. Thanks.

Ajay

Serial Number,Model,Name,Number,Time of Reading,Sample Rate,Start Time
910000000ebeaa4,DS1922T,SRBA04-240909  ,beaa-TNTVLPRP54,9/30/09 16:52,1,9/24/09 18:59
Sample #,Date/Time,Temp ¡F,Temp ¡C,,,
1,"Thu, Sep, 24, 06:59, PM",82.4,28,,,
2,"Thu, Sep, 24, 07:00, PM",83.3,28.5,,,
3,"Thu, Sep, 24, 07:01, PM",83.3,28.5,,,
4,"Thu, Sep, 24, 07:02, PM",83.3,28.5,,,
5,"Thu, Sep, 24, 07:03, PM",83.3,28.5,,,
6,"Thu, Sep, 24, 07:04, PM",83.3,28.5,,,
7,"Thu, Sep, 24, 07:05, PM",83.3,28.5,,,
8,"Thu, Sep, 24, 07:06, PM",83.3,28.5,,,
9,"Thu, Sep, 24, 07:07, PM",83.3,28.5,,,
10,"Thu, Sep, 24, 07:08, PM",83.3,28.5,,,

solution, based on deceze's comment below

deceze, thanks. this works [as does the sql insert]. much appreciated.

<?php
$fhandle = fopen('1.csv', 'r');

$device_header = fgetcsv($fhandle, 1000, ',');                      
$device_data = fgetcsv($fhandle,1000,',');                          

$device = array_combine($device_header, $device_data);          

$data_header = fgetcsv($fhandle,1000,',');                          

$dataset = array();                                             
    while ($data = fgetcsv($fhandle,1000,',')) {  
        $dataset[] = array_combine($data_header, $data);        
}

fclose($fhandle);

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傾旎 2024-09-17 17:29:00

像这样的事情应该这样做:

$fhandle = fopen('/path/to/input.csv', 'r');

$headers = fgetcsv($fhandle);  // reads first line
$set1 = array_combine($headers, fgetcsv($fhandle));  // reads second line

$set2 = array();
$headers = fgetcsv($fhandle);  // reads third line
while ($data = fgetcsv($fhandle)) {  // reads all following lines
    $set2[] = array_combine($headers, $data);
}

fclose($fhandle);

/*
$set1 == array('Serial Number' => '910000000ebeaa4', 'Model' => ...);
$set2 == array(
    array('Sample #' => 1, 'Date/Time' => 'Thu, Sep, 24, 06:59, PM', ...),
    ...
);
*/

Something like this should do it:

$fhandle = fopen('/path/to/input.csv', 'r');

$headers = fgetcsv($fhandle);  // reads first line
$set1 = array_combine($headers, fgetcsv($fhandle));  // reads second line

$set2 = array();
$headers = fgetcsv($fhandle);  // reads third line
while ($data = fgetcsv($fhandle)) {  // reads all following lines
    $set2[] = array_combine($headers, $data);
}

fclose($fhandle);

/*
$set1 == array('Serial Number' => '910000000ebeaa4', 'Model' => ...);
$set2 == array(
    array('Sample #' => 1, 'Date/Time' => 'Thu, Sep, 24, 06:59, PM', ...),
    ...
);
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文