Created
November 20, 2013 10:45
-
-
Save anonymous/7561146 to your computer and use it in GitHub Desktop.
Example of malfunctioning code where SplFileObject read_csv treats multi line csv data fields as a new line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$skus = new CSVFile('var/import/data.csv'); | |
foreach($skus as $line){ | |
echo $line['sku']; | |
echo $line['description']; | |
} | |
class CSVFile extends SplFileObject { | |
private $keys; | |
public function __construct($file) | |
{ | |
parent::__construct($file); | |
$this->setFlags(SplFileObject::READ_CSV); | |
$this->setFlags(SplFileObject::READ_AHEAD); | |
} | |
public function rewind() | |
{ | |
parent::rewind(); | |
$this->keys = parent::current(); | |
parent::next(); | |
} | |
public function current() | |
{ | |
return array_combine($this->keys, parent::current()); | |
} | |
public function getKeys() | |
{ | |
return $this->keys; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment