-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseCSV.php
36 lines (31 loc) · 956 Bytes
/
parseCSV.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
// Created by Yehuda Eisenberg
function parseCsv($filename, $title = true){
$dataText = file_get_contents($filename);
$dataArr = explode("\n", $dataText);
if($title){
$names = explode(",", $dataArr[0]);
unset($dataArr[0]);
}
$res = array();
foreach($dataArr as $data){
$data = explode(",", $data);
$tmp = array();
foreach($data as $i => $dat){
if($title){
if(isset($tmp[trim($names[$i])])){
if(is_string($tmp[trim($names[$i])]))
$tmp[trim($names[$i])] = array($tmp[trim($names[$i])], trim($dat));
else
$tmp[trim($names[$i])][] = trim($dat);
}
else
$tmp[trim($names[$i])] = trim($dat);
}
else
$tmp[$i] = trim($dat);
}
$res[] = $tmp;
}
return $res;
}