Skip to content

Instantly share code, notes, and snippets.

Created November 20, 2013 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7561146 to your computer and use it in GitHub Desktop.
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
<?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