我有一些不同的CSV文件,其中包含由WR
分隔的输入块我想用数据制作四个独立的数组。
例如
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 :(得分:1)
如果我理解你需要什么,你可以使用php中的explode方法将你的字符串从csv文件拆分成一个数组。使用;作为分隔符:)
我希望这会有所帮助。
答案 1 :(得分: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 );
}
?>
答案 2 :(得分:0)
你可以通过exploding
数组
$filename=explode(";",$string);
答案 3 :(得分: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
}
}
?>