-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_read_csv.php
68 lines (62 loc) · 1.98 KB
/
lib_read_csv.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!--
+Description: PHP file that allows users to contact the staff for feedback.
+Created by : Ben Birney
+Created on : 27 NOV 2017
+Last Modified by: Ben Birney
+Last Modified on: 11 DEC 2017
+Modified by: Ben Birney
+-->
<?php
// This function reads in a CSV, Comma Seperated file, and returns a
// 2D array indexed like, $table[ROW_ID][COLUMN_ID] = CELL_VALUE
// Inputs:
// $filename = 'test.csv' // specific file
// $withHeaders = True // Is their a header row
// $withLeftID = True // Can a row be determined by the leftmost column
// $withDelimiter = ',' // What is the default delimiter
function read_csv($filename, $withHeaders=True, $withLeftID=True, $withDelimiter=',') {
if ($fp = fopen($filename, 'r')) {
$counter = 0;
while($row = fgetcsv($fp, 0, $withDelimiter)) {
if (!isset($headers) && !$withHeaders) {
$headers = array_keys($row);
}
if (!isset($headers)) {
$headers = $row;
} else {
foreach ($row as $i => $value) {
if ($withLeftID) {
$table[$row[0]][$headers[$i]] = $value;
} else {
$table[$counter][$headers[$i]] = $value;
}
}
}
$counter++;
}
}
return $table;
}
// function: allows writing to a csv file while retaining the format given by read_csv().
function write_csv($filename, $data=array(), $withHeaders=True, $withLeftID=True, $leftID='', $withDelimiter=',') {
if ($withHeaders) {
$headers = get_csv_headers($filename);
}
if($fp = fopen($filename, 'w')) {
if ($withHeaders) {
fputcsv($fp, $headers, $withDelimiter);
}
foreach ($data as $row) {
if ($row[$headers[0]] != "") {
fputcsv($fp, $row, $withDelimiter);
} else if ($withHeaders == False) {
fputcsv($fp, $row, $withDelimiter);
}
}
fclose($fp);
return True;
} else {
return False;
}
}
?>