2012-10-04 27 views
0

獲取CSV的塊我有包含由WRPHP - 以頭名

分離輸入我想要四個獨立陣列與數據塊不同部分的CSV文件。

EG。

Datum;Uhrzeit;WR;Status;SolIrr;TmpMod;TmpAmb;Wind;DaySumIrr;WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac;WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac;WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac 

將前來四個陣列。然後

WR;Status;SolIrr;TmpMod;TmpAmb;Wind;DaySumIrr; 
WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac; 
WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac; 
WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac; 

四個陣列將被插入到四個不同的MySQL表。

我已經設法正確地將標題拆分爲四個數組,但我不知道如何將csv中的每行數據拆分爲單獨的數組。

我希望我有道理。

感謝

+0

數據行中WR列的相應值是什麼?它是空白還是WR? – ace

+0

這只是WR .​​. – user1719857

+0

我還應該補充說,WR塊以不同的順序出現。 – user1719857

回答

1

如果我理解正確的話,你需要什麼,你可以使用從PHP的explode方法從CSV文件分割你的字符串到一個數組。使用;作爲分隔符:)

我希望這有助於。

0

使用內置在explode功能是這樣的:

<?php 

$string = 'WR;Status;SolIrr;TmpMod;TmpAmb;Wind;DaySumIrr; 
WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac; 
WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac; 
WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac;'; 

# convert into lines 
$lines = explode(PHP_EOL, $string); 

# convert into items 
$items = array(); 
foreach ($lines as $line) { 
    $items[] = explode(';', $line); 
} 

?> 
0

您可以通過exploding做到這一點的陣列

$filename=explode(";",$string); 
0

根據我從你的問題明白了,這是我想出瞭解決方案用。首先,根據標題得出WR如何排序,然後生成偏移長度的鍵值對。使用該鍵值對來切割從每個csv行分解的數據數組。

<?php 

    $headers = "Datum;Uhrzeit;WR;Status;SolIrr;TmpMod;TmpAmb;Wind;DaySumIrr;WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac;WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac;WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac"; 
    $headers_arr = explode(";", $headers); 

    // track where WR is positioned in the array 
    $WR_offset = array(); 
    foreach ($headers_arr as $i => $value) { 
    if ($value == "WR") { $WR_offset[] = $i; } 
    } 
    $WR_offset[] = count($headers_arr); // assume imaginary WR as last header 

    // loop through the WR_offset array, 
    // to get the WR position, and the number of fields before the next WR position 
    // to be used in array_slice 
    for ($i = 0; $i < count($WR_offset) - 1; $i++) { 
    $offset = $WR_offset[$i] + 1; // 
    $length = $WR_offset[$i+1] - $WR_offset[$i] - 1; 
    // store the offset and length as key-value pair 
    $slice_params[$offset] = $length; 
    } 

    // assuming $lines contains the CSV data rows, and $line is a single CSV data row 
    foreach ($lines as $line) { 
    $row_array = explode(";", $line); 
    // loop through the generated offset-length value pair 
    foreach ($slice_params as $offset => $length) { 
     $array_chunks = array_slice($row_array, $offset, $length); 
     // do what you want to do with the chunk here 
    } 
    } 

?> 
+0

這看起來可能是我以後的樣子。 – user1719857

+0

我會將它加入腳本並回報。 – user1719857