-
Notifications
You must be signed in to change notification settings - Fork 0
/
coma.php
36 lines (32 loc) · 812 Bytes
/
coma.php
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
31
32
33
34
35
36
<?php
Class Coma {
public function __construct($csv) {
$this->csv = $csv;
}
public function parse() {
$i = null;
list($headers, $this->csv) = explode("\n", $this->csv, 2);
$headers = str_getcsv($headers, ',', '"');
foreach ($headers as $header) {
$this->{$header} = [];
$this->keymap[(Int)$i++] = $header;
}
$i = null;
$lines = explode("\n", $this->csv);
foreach ($lines as $line ) {
$columns = str_getcsv($line, ',', '"');
if (count($columns) > count($this->keymap)) {
continue;
}
foreach ($columns as $column) {
$num = (Int)$i++;
$header = $this->keymap[$num];
$data[$header] = $column;
}
$this->data[] = $data;
$data = null;
$i = null;
}
return $this->data;
}
}