简体   繁体   中英

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 ; 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; in the Csvimport class at the very end to test the data import and this is what it gives me:

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.

This is my csv file:

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

This is my csv import library file:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM