-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsv.php
49 lines (39 loc) · 1006 Bytes
/
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
<?php
/**
* Created by PhpStorm.
* User: diwi
* Date: 07.06.2015
* Time: 17:17
*/
class Csv
{
public static function loadFileToArray($file)
{
$output = array();
$file = file($file);
foreach ($file as $line) {
$row = explode(',', $line);
foreach ($row as $key => $value) {
if ($value == 'Inf') {
$row[$key] = INF;
} else if ($value == '-Inf') {
$row[$key] = -INF;
} else {
$row[$key] = floatval($value);
}
}
$output[] = $row;
}
return $output;
}
public static function saveArrayToFile($data, $file)
{
$stream = fopen('data://text/plain,' . "", 'w+');
foreach ($data as $val) {
fputcsv($stream, $val);
}
rewind($stream);
file_put_contents($file, stream_get_contents($stream));
fclose($stream);
}
}