SplFileObject Extended class issue
I came across this class somewhere on Stack and have seem to come up with
a bit of an error. I'm attempting to read a csv to an associated array
where the first row is a header row and the rest are data. Oddly this was
working fine until this morning and I have no idea what has went wrong.
PHP:
class CSVFile extends SplFileObject{
private $keys;
public function __construct($file){
parent::__construct($file);
$this->setFlags(SplFileObject::READ_CSV);
}
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;
}
}
$csv = new CSVFile($csvfile);
foreach ($csv as $linek => $linev){
// Do stuff
}
Expected Output:
Array(2){
[0] => Array(3){
['header1'] => 'value1',
['header2'] => 'value2',
['header3'] => 'value3'
}
[1] => Array(3){
['header1'] => 'value4',
['header2'] => 'value5',
['header3'] => 'value6'
}
[2] => Array(3){
['header1'] => 'value7',
['header2'] => 'value8',
['header3'] => 'value9'
}
}
Actual Output:
object(CSVFile)#1 (6) {
["keys":"CSVFile":private]=> NULL
["pathName":"SplFileInfo":private]=> string(7) "abc.csv"
["fileName":"SplFileInfo":private]=> string(7) "abc.csv"
["openMode":"SplFileObject":private]=> string(1) "r"
["delimiter":"SplFileObject":private]=> string(1) ","
["enclosure":"SplFileObject":private]=> string(1) """
}
No comments:
Post a Comment