[英]How to parse a csv file where each row has multiple columns?
This one is a bit long, so I apologize in advance. 这个有点长,所以我事先表示歉意。
I'm trying to parse a csv file with a ; 我正在尝试使用;解析csv文件。 delimiter and for some reason it's only parsing the first string and then moving on to the next row in the file.
定界符,由于某种原因,它仅解析第一个字符串,然后移至文件中的下一行。 I did a
print_r($content); break;
我做了一个
print_r($content); break;
print_r($content); break;
in the Csvimport class at the very end to test the data import and this is what it gives me: 在最后的Csvimport类中测试数据导入,这就是给我的:
Array ( [1] => Array ( [Key1] => Key1 ) [2] => Array ( [Key2] => Key2 ) ) 1
That's one problem. 那是一个问题。 The second is, when I let it go to the view, it throws errors for each line where
$field['key']
, $field['address']
, $field['lotno']
, etc... is saying that key, address, lotno are undefined. 第二个是,当我进入视图时,它会为
$field['key']
, $field['address']
, $field['lotno']
等的每一行引发错误。该键,地址,lotno未定义。
This is my csv file: 这是我的csv文件:
Key1;Address1;LotNo1;Acres1;YrBuilt1;
Key2;Address2;LotNo2;Acres2;YrBuilt2;
This is my csv import library file: 这是我的csv导入库文件:
class Csvimport {
var $fields;/** columns names retrieved after parsing */
var $separator = ';';/** separator used to explode each line */
var $enclosure = '"';/** enclosure used to decorate each field */
var $max_row_size = 4096;/** maximum row size to be used for decoding */
function parse_file($p_Filepath) {
$file = fopen($p_Filepath, 'r');
$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
$keys = str_getcsv($this->fields[0]);
$i = 1;
while (($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false) {
if ($row != null) { // skip empty lines
$values = str_getcsv($row[0]);
if (count($keys) == count($values)) {
$arr = array();
for ($j = 0; $j < count($keys); $j++) {
if ($keys[$j] != "") {
$arr[$keys[$j]] = $values[$j];
}
}
$content[$i] = $arr;
$i++;
}
}
}
fclose($file);
// echo print_r($content); break;
return $content;
}
}
This is my controller: 这是我的控制器:
$import = './application/imports/unitimport.csv';
$this->load->library('csvimport');
$data['importdata'] = $this->csvimport->parse_file($import);
And this is my view: 这是我的观点:
<?php foreach($importdata as $field): ?>
<tr>
<td><p><?php echo $field['key'] ?><p></td>
<td><p><?php echo $field['address'] ?><p></td>
<td><p><?php echo $field['lotno'] ?><p></td>
<td><p><?php echo $field['acres'] ?><p></td>
<td><p><?php echo $field['yrbuilt'] ?><p></td>
</tr>
<?php endforeach ?>
What am I missing? 我想念什么?
This is an old question... but here you go: 这是一个古老的问题...但是在这里您可以:
This line: 这行:
$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
saves an the first line of your file to an array in $this->fields. 将文件的第一行保存到$ this-> fields中的数组。 So at this point:
因此,在这一点上:
$this->fields = array('Key1','Address1','LotNo1','Acres1','YrBuilt1');
On the next line when you: 在下一行中,当您:
$keys = str_getcsv($this->fields[0]);
str_getcsv() is parsing the first value of $this-fields
and converting it to an array. str_getcsv()正在解析
$this-fields
的第一个值并将其转换为数组。 So at this point: 因此,在这一点上:
$keys = array('Key1');
You repeat this same mistake when you iterate through the rows of data with this line: 使用以下行遍历数据行时,您会重复同样的错误:
$values = str_getcsv($row[0]);
$values
only stores the first data column because thats the only item that was parsed. $values
仅存储第一个数据列,因为那是解析的唯一项目。
The following code should work: 下面的代码应该工作:
class Csvimport
{
var $fields;/** columns names retrieved after parsing */
var $separator = ';';/** separator used to explode each line */
var $enclosure = '"';/** enclosure used to decorate each field */
var $max_row_size = 4096;/** maximum row size to be used for decoding */
function parse_file($p_Filepath)
{
$file = fopen($p_Filepath, 'r');
$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
$content = array();
$i = 1;
while (($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false)
{
if ($row != null) // skip empty lines
{
for ($j = 0; $j < count($this->fields); $j++)
{
$content[$i][$this->fields[$j]] = $row[$j];
}
$i++;
}
}
fclose($file);
return $content;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.