forked from goodby/csv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsv-sample.php
More file actions
30 lines (23 loc) · 786 Bytes
/
Copy pathtsv-sample.php
File metadata and controls
30 lines (23 loc) · 786 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
require_once __DIR__.'/../vendor/autoload.php'; // load composer
use Goodby\CSV\Import\Standard\Lexer;
use Goodby\CSV\Import\Standard\Interpreter;
use Goodby\CSV\Import\Standard\LexerConfig;
// the result comes into this variable
$temperature = array();
// set up lexer
$config = new LexerConfig();
$config->setDelimiter("\t");
$config->setFlags(\SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::READ_CSV);
$lexer = new Lexer($config);
// set up interpreter
$interpreter = new Interpreter();
$interpreter->addObserver(function(array $row) use (&$temperature) {
$temperature[] = array(
'temperature' => $row[0],
'city' => $row[1],
);
});
// parse
$lexer->parse('temperature.tsv', $interpreter);
var_dump($temperature);