diff --git a/arrayToCSV.php b/arrayToCSV.php
new file mode 100644
index 0000000..638abe4
--- /dev/null
+++ b/arrayToCSV.php
@@ -0,0 +1,60 @@
+
\ No newline at end of file
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..b26573f
--- /dev/null
+++ b/index.html
@@ -0,0 +1,2 @@
+
JSON 2 CSV
+ Click here to convert your json to csv
\ No newline at end of file
diff --git a/json2csv.class.php b/json2csv.class.php
index 20e3c5d..5a03f97 100644
--- a/json2csv.class.php
+++ b/json2csv.class.php
@@ -1,5 +1,10 @@
dataArray;
}
- function JSONfromFile($file){
- $this->dataArray = json_decode(file_get_contents($file),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;
}
- private function prependColumnNames(){
- foreach(array_keys($this->dataArray[0]) as $key){
- $keys[0][$key] = $key;
+ 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 save2CSV($file){
+ function browserDL($CSVname){
+
if($this->isItNested() || !is_array($this->dataArray)){
- echo "JSON is either invalid or has nested elements.";
+ echo "JSON is either invalid or you encountered an edge case I missed. Please let me know at arelangi@gmail.com";
}
else{
- $fileIO = fopen($file, 'w+');
+ header("Content-Type: text/csv; charset=utf-8");
+ header("Content-Disposition: attachment; filename=$CSVname");
+
+ $output = fopen('php://output', 'w');
+
foreach ($this->dataArray as $fields) {
- fputcsv($fileIO, $fields);
+ fwrite($output,arrayToCSV($fields)."\n");
}
- fclose($fileIO);
}
}
- function browserDL($CSVname){
+
+
+ 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 "JSON is either invalid or has nested elements.
";
+ echo "JSON is either invalid or you encountered an edge case I missed. Please let me know at arelangi@gmail.com";
}
else{
- header("Content-Type: text/csv; charset=utf-8");
- header("Content-Disposition: attachment; filename=$CSVname");
- $output = fopen('php://output', 'w');
+ echo 'orey '.$file;
+ ob_flush();
+ $fileIO = fopen($file, 'w+');
foreach ($this->dataArray as $fields) {
- fputcsv($output, $fields);
+ fputcsv($fileIO, $fields);
}
+ fclose($fileIO);
}
}
-
- private function isItNested(){
+
+ function isItNested(){
foreach($this->dataArray as $data){
if(is_array($data)){
$isNested = TRUE;
diff --git a/json2csv.php b/json2csv.php
index c5de3ec..1d91236 100644
--- a/json2csv.php
+++ b/json2csv.php
@@ -1,5 +1,7 @@
savejsonFile2csv($arguments["file"], $filepath);
}
- elseif($_FILES["file"]["type"] != NULL){
+ elseif(($_FILES["file"]["type"]) != NULL){
+
$JSON2CSV->JSONfromFile($_FILES["file"]["tmp_name"]);
- $JSON2CSV->browserDL("JSON2.CSV");
+ $JSON2CSV->browserDL(str_replace("json", "csv", $_FILES["file"]["name"]));
}
elseif($_POST['json'] != NULL){
+
$JSON2CSV->readJSON($_POST['json']);
- $JSON2CSV->browserDL("JSON2.CSV");
+ $JSON2CSV->browserDL("JSON.CSV");
}
}
else{
@@ -38,7 +43,7 @@