English 中文(简体)
CSV 拥有多个头盔;使用php进口到我的舱里
原标题:CSV with multiple header rows; import into mysql using php

I ve got a CSV file with two set of data in it [itsdraed a templogger]. 我很想知道,是否有办法将头两行作为一组数据和其余数据作为第二组数据输入。 我熟悉购买力平价,但从来没有做过这样的事情。 兹附上下一组数据。 任何帮助或方向都将受到高度赞赏。 感谢。

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,,,

解决办法

感谢。 [原文照搬]。 非常感谢。

<?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);

?>
最佳回答

这样做有点:

$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 , ...),
    ...
);
*/
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签