Created
March 10, 2016 07:07
-
-
Save useless-stuff/ffcfb0fe86b15167aa6f to your computer and use it in GitHub Desktop.
PHP - SplFileObject
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
John | Busha | developer | |
---|---|---|---|
Mark | Boiler | graphic designer | |
Mario | Rossi | developer |
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 | |
Class Employee{ | |
public $name, $surname, $role; | |
public function __toString() | |
{ | |
return $this->name.' '.$this->surname.' '.$this->role; | |
} | |
} | |
$data = new SplFileObject('data.csv'); | |
$data->setFlags(SplFileObject::READ_CSV); | |
$employeesCollection = new ArrayIterator(); | |
foreach($data as $record){ | |
$employee = new Employee(); | |
$employee->name = $record[0]; | |
$employee->surname = $record[1]; | |
$employee->role = $record[2]; | |
$employeesCollection->append($employee); | |
} | |
foreach($employeesCollection as $employee){ | |
echo $employee.PHP_EOL; | |
} | |
// Result: | |
/* | |
John Busha developer | |
Mark Boiler graphic designer | |
Mario Rossi developer | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment