-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathjson2csv.class.php
More file actions
131 lines (99 loc) · 2.61 KB
/
json2csv.class.php
File metadata and controls
131 lines (99 loc) · 2.61 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
require_once('arrayToCSV.php');
ini_set('memory_limit', '-1');
class JSON2CSVutil{
public $dataArray;
public $isNested = FALSE;
function readJSON($JSONdata){
$this->dataArray = json_decode($JSONdata,1);
$this->prependColumnNames();
return $this->dataArray;
}
function isNested($array){
foreach($array as $data){
if(is_array($data)){
return TRUE;
}
}
return FALSE;
}
function getAllKeys($array){
$result = array();
$children = array();
foreach($array as $sub=>$value) {
if(gettype($value)=='array'){
$children[] = $value;
//$result = array_merge($result,$this->getAllKeys($value));
}else{
$result [] = $sub;
}
}
foreach ($children as $key => $value) {
$result = array_merge($result,$this->getAllKeys($value));
}
return $result;
}
function prependColumnNames(){
$keys = array();
foreach ($this->dataArray as $key => $value) {
if($this->isNested($value)){
$keys[] = $this->getAllKeys($this->dataArray[$key]);
break;
}
}
if(count($keys)==0){
$keys[] = $this->getAllKeys($this->dataArray[0]);
}
$this->dataArray = array_merge($keys, $this->dataArray);
}
function browserDL($CSVname){
if($this->isItNested() || !is_array($this->dataArray)){
echo "<h1>JSON is either invalid or you encountered an edge case I missed. Please let me know at arelangi@gmail.com<h1>";
}
else{
header("Content-Type: text/csv; charset=utf-8");
header("Content-Disposition: attachment; filename=$CSVname");
$output = fopen('php://output', 'w');
foreach ($this->dataArray as $fields) {
fwrite($output,arrayToCSV($fields)."\n");
}
}
}
function JSONfromFile($file){
$this->dataArray = json_decode(file_get_contents($file),TRUE);
$this->prependColumnNames();
return $this->dataArray;
}
function save2CSV($file){
if($this->isItNested() || !is_array($this->dataArray)){
echo "<h1>JSON is either invalid or you encountered an edge case I missed. Please let me know at arelangi@gmail.com<h1>";
}
else{
echo 'orey '.$file;
ob_flush();
$fileIO = fopen($file, 'w+');
foreach ($this->dataArray as $fields) {
fputcsv($fileIO, $fields);
}
fclose($fileIO);
}
}
function isItNested(){
foreach($this->dataArray as $data){
if(is_array($data)){
$isNested = TRUE;
break 1;
}
}
return $this->isNested;
}
function savejson2csv($JSONdata, $file){
$this->readJSON($JSONdata);
$this->save2CSV($file);
}
function savejsonFile2csv($file, $destFile){
$this->JSONfromFile($file);
$this->save2CSV($destFile);
}
}
?>