A simple CSV file reader.

Emulates an array of records (arrays), implimented as an Iterator, so can be used in a foreach statements.

  • If your CSV file has headers (the default), then the keys of the returned array will be the header values.
  • You can also specify a different field delimiter, for example ("\t") for tabs.
  • Use rewind to reset to the top of the file.
  • The header record is NEVER returned as a record. The first iteration will be the first record in the file, excluding the header record if specified.
  • implements \Iterator<array<string, string>>
public __construct(string $fileName, bool $headerRow = true, string $delimiter = ',')
public __destruct()
public current() : array
  • return array<string,string>
public key() : int
public next() : void
public rewind() : void
public setHeaders(array $headers) : static

You can specify headers if your file does not include them. The headers will be used as the key in the returned associative array for each record.

  • param string[] $headers of strings
public valid() : bool
private array $current = []
  • var array<string,string>
private string $delimiter
private $fh = false
private string $fileName
private bool $headerRow
private array $headers = []
  • var string[]
private int $index = 0
private closeFile() : static
© 2024 Bruce Wells
Search Namespaces \ Classes