-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSaveGoogleArrayToJson.php
91 lines (72 loc) · 2.26 KB
/
SaveGoogleArrayToJson.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
class SaveGoogleArrayToJson {
private $array = array();
private $xData = array();
private $yData = array();
private $xyData = array();
public function __construct(Array $data) {
$this->array = $data;
$this->setXYData();
}
public function setXYData() {
// might be able to use array_column if I get apply changes
// in a functional way
foreach ($this->array as $key => $value) {
$this->yData[] = $value[2];
$x = $value[0] . " " . $value[1] . "00"; //20130922 1400
$xt = $this->setXDataToTime($x);
$this->xData[] = $xt;
$this->xyData[] = array($xt, $value[2]);
}
}
public function getXData() {
return $this->xData;
}
public function getYData() {
return $this->yData;
}
public function getXYData() {
return $this->xyData;
}
// conver x data to dateTime OBject
public function setXDataToTime($timeString) {
$dateTime = DateTime::createFromFormat('Ymj Hi', $timeString);
return $dateTime;
}
//save x,y to json file
public function setDataToJson($array) {
return json_encode($array);
}
public function getDataFromJson($jsonData) {
return json_decode($jsonData);
}
public function saveStringtoFile($string, $filename) {
$fp = fopen($filename, 'w');
fwrite($fp, $string);
fclose($fp);
}
public function loadFiletoJson($filename) {
$string = file_get_contents($filename);
$json_a = json_decode($string, true);
return $json_a;
}
public function getYMean() {
$sum = (int) array_sum($this->yData);
$count = (int) count($this->yData);
$this->yMean = $sum / $count;
return $this->yMean;
}
public function removeMeanfromYdata() {
// do it with a lambda
$zeroMean = $this->yMean;
$func = function($value) use ($zeroMean) {
return $value - $zeroMean;
};
return $this->yZeroMean = array_map($func, $this->yData);
}
}