如何解析每行有多列的csv文件?

时间:2016-03-19 15:52:04

标签: php codeigniter csv

这个有点长,所以我提前道歉。

我试图用c解析csv文件;分隔符,由于某种原因,它只解析第一个字符串,然后继续前进到文件中的下一行。我在最后的Csvimport类中做了print_r($content); break;来测试数据导入,这就是它给我的:

Array ( [1] => Array ( [Key1] => Key1 ) [2] => Array ( [Key2] => Key2 ) ) 1

这是一个问题。第二个是,当我让它进入视图时,它会为$field['key']$field['address']$field['lotno']等所在的每一行引发错误,表示密钥,地址,lotno未定义。

这是我的csv文件:

Key1;Address1;LotNo1;Acres1;YrBuilt1;
Key2;Address2;LotNo2;Acres2;YrBuilt2;

这是我的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;
    }
}

这是我的控制者:

$import = './application/imports/unitimport.csv';
$this->load->library('csvimport');
$data['importdata'] = $this->csvimport->parse_file($import);

这是我的观点:

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

我错过了什么?

1 个答案:

答案 0 :(得分:1)

这是一个老问题......但是你走了:

这一行:

$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);

将文件的第一行保存到$ this-&gt;字段中的数组中。所以在这一点上:

$this->fields = array('Key1','Address1','LotNo1','Acres1','YrBuilt1');

在下一行:

$keys = str_getcsv($this->fields[0]);

str_getcsv()正在解析$this-fields的第一个值并将其转换为数组。所以在这一点上:

$keys = array('Key1');

当您使用以下行迭代数据行时,您会重复同样的错误:

$values = str_getcsv($row[0]);

$values仅存储第一个数据列,因为这是唯一被解析的项目。

以下代码应该有效:

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;
    }
}