PHP-fgetcsv循环逐列从csv文件获取数据 [英] PHP - fgetcsv loop for get data from csv file column by column
问题描述
我有一个31列24行的CSV文件.我需要使用fgetcsv
(或者可能是其他解决方案)来传递CSV并逐列获取数据.我有一个解决方案:
I have a CSV file with 31 columns and 24 rows. I need with fgetcsv
(or maybe another solution) to pass in the CSV and get data column by column. I have one solution:
// { ?
for ($col=1; $col<=32; $col++) {
while ($data = fgetcsv($read, max, ";")) {
$row[] = $data[$col];
}
print_r($row);
//... in the line behind i have sql query to insert data in base.
}
unset($row);
}
for循环正在工作,但是$data[$col]
仅从CSV的第一列获取数据,而当$col
为2
,3
,4
, ...,31
.
The for loop is working but $data[$col]
only gets data from the first column of the CSV, it doesn't get data from $data[$col]
when $col
is 2
, 3
, 4
, ..., 31
.
我在哪里犯了一个错误,这是什么问题?该代码对我来说似乎还不错,但我认为我对fgetcsv
函数一无所知.
Where did I make a mistake, and what is the problem? The code looks okay to me, but I think that I don't know something about the fgetcsv
function.
max是CSV的文件大小,fgetcsv
类似于文件的最大大小.不管fgetcsv
中的内容是什么,他们只会看到第一列,而不会在每次$col
更改时循环.
max is filesize of CSV, fgetcsv
see that like max size of file. No matter what is in fgetcsv
they see just first column, not loop every time when $col
changes.
insert = "INSERT INTO xxxx (v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24) VALUES ('$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]','$row[7]','$row[8]' '$row[9]','$row[10]','$row[11]','$row[12]','$row[13]','$row[14]','$row[15]','$row[16]','$row[17]','$row[18]','$row[19]','$row[20]','$row[21]','$row[22]','$row[23]','$row[24]')"
推荐答案
在您的代码中:
for ($col=1; $col<=32; $col++) {
while ($data = fgetcsv($read, max, ";")) {
您正尝试从文件中提取所有行32次.第一次迭代从文件中获取所有行,因为while
循环在fgetcsv
返回false
时(没有更多数据可读取时)结束.
you are trying to fetch all rows from the file 32 times. The first iteration fetches all the rows from the file, since the while
loop ends when fgetcsv
returns false
(when there is no more data to read).
我需要使用
fgetcsv
(或其他解决方案)来传递csv并逐列获取数据.
I need with
fgetcsv
(or maybe other solution) to pass in csv and get data column by column.
您无法逐列读取数据,因为文件是逐行读取的,并且这些行代表表的行.如果要按列迭代表数据,可以将整个表读入数组,然后交换行和单元格:
You can not read data column by column, since the file is read line by line, and the lines represent the table rows. If you want to iterate the table data by columns, you can read the entire table into an array, then exchange rows and cells:
$table = [];
while ($row = fgetcsv($fp)) {
$table []= $row;
}
for ($r = 0; $r < count($table); $r++) {
for ($c = 0; $c < count($table[$r]); $c++) {
$new_table[$c][$r] = $table[$r][$c];
}
}
// Use $new_table to iterate the table "by columns"
foreach ($new_table as $column) {
foreach ($column as $cell) {
$fields []= $cell;
}
$fields = implode(',', $cell);
// do_something($fields)
}
因此,您不需要外部循环来迭代索引"列,因为fgetcsv
已经为下一行包含项包含单元格值的行获取了一个数组:
So you don't need the outer loop iterating the column "indices", as fgetcsv
already fetches an array for the next row where the items contain the cell values:
$row = fgetcsv($fp);
// $row = ['column 0 cell', 'column 1 cell', ... ]
至少,将循环放入提取行的while
循环中.如果要迭代单元格,请迭代fgetcsv
返回的值.
At least, put the loop into the while
loop that fetches the rows. If you want to iterate the cells, iterate the value returned by fgetcsv
.
示例
$fp = fopen('test.csv', 'w+');
for ($i = 0; $i < 4; $i++) {
for ($j = 0; $j < 6; $j++) {
$a[$i][$j] = "r $i c $j";
}
fputcsv($fp, $a[$i]);
}
rewind($fp);
while ($row = fgetcsv($fp)) {
echo implode(' | ', $row), PHP_EOL;
// Or iterate $row, if you need:
// foreach ($row as $cell) { do_something($cell); }
}
fclose($fp);
输出
r 0 c 0 | r 0 c 1 | r 0 c 2 | r 0 c 3 | r 0 c 4 | r 0 c 5
r 1 c 0 | r 1 c 1 | r 1 c 2 | r 1 c 3 | r 1 c 4 | r 1 c 5
r 2 c 0 | r 2 c 1 | r 2 c 2 | r 2 c 3 | r 2 c 4 | r 2 c 5
r 3 c 0 | r 3 c 1 | r 3 c 2 | r 3 c 3 | r 3 c 4 | r 3 c 5
这篇关于PHP-fgetcsv循环逐列从csv文件获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!