Array to CSV file for vertical layout and failed to put the header row

会有一股神秘感。 提交于 2021-02-19 07:45:07

问题


I have a problem to create a horizontal layout for the csv

here is an array which I have convert it to json-format

{"datetime":["2000-01-01","2000-01-02","2000-01-03","2000-01-04","2000-01-05","2000-01-06","2000-01-07","2000-01-08","2000-01-09","2000-01-10","2000-01-11","2000-01-12","2000-01-13","2000-01-14","2000-01-15","2000-01-16","2000-01-17","2000-01-18","2000-01-19","2000-01-20","2000-01-21","2000-01-22","2000-01-23","2000-01-24","2000-01-25","2000-01-26","2000-01-27","2000-01-28","2000-01-29","2000-01-30","2000-01-31","2000-02-01","2000-02-02","2000-02-03","2000-02-04","2000-02-05","2000-02-06","2000-02-07","2000-02-08","2000-02-09","2000-02-10","2000-02-11","2000-02-12","2000-02-13","2000-02-14","2000-02-15","2000-02-16","2000-02-17","2000-02-18","2000-02-19","2000-02-20","2000-02-21","2000-02-22","2000-02-24","2000-02-25","2000-02-26","2000-02-27","2000-02-28","2000-02-29","2000-03-01","2000-03-02","2000-03-03","2000-03-04","2000-03-05","2000-03-06","2000-03-07","2000-03-08","2000-03-09","2000-03-10","2000-03-11","2000-03-12","2000-03-13","2000-03-14","2000-03-15","2000-03-16","2000-03-17","2000-03-18","2000-03-19","2000-03-22","2000-03-23","2000-03-24","2000-03-28","2000-03-29","2000-03-30","2000-03-31","2000-04-01","2000-04-02","2000-04-03","2000-04-04","2000-04-05","2000-04-06","2000-04-07","2000-04-08","2000-04-09","2000-04-10","2000-04-11","2000-04-12","2000-04-13","2000-04-14","2000-04-15","2000-04-16","2000-04-17","2000-04-18","2000-04-19","2000-04-20","2000-04-21","2000-04-22","2000-04-23","2000-04-24","2000-04-25","2000-04-26","2000-04-27","2000-04-28","2000-04-29","2000-04-30","2000-05-01","2000-05-02","2000-05-03","2000-05-04","2000-05-05","2000-05-06","2000-05-07","2000-05-08","2000-05-09","2000-05-10","2000-05-11","2000-05-12","2000-05-13","2000-05-14","2000-05-15","2000-05-16","2000-05-17","2000-05-18","2000-05-19","2000-05-20","2000-05-21","2000-05-22","2000-05-23","2000-05-24","2000-05-25","2000-05-26","2000-05-27","2000-05-28","2000-05-29","2000-05-30","2000-05-31","2000-06-01","2000-06-02","2000-06-03","2000-06-04","2000-06-05","2000-06-06","2000-06-07","2000-06-08","2000-06-09","2000-06-10","2000-06-11","2000-06-12","2000-06-13","2000-06-14","2000-06-15","2000-06-16","2000-06-17","2000-06-18","2000-06-19","2000-06-20","2000-06-21","2000-06-22","2000-06-23","2000-06-24","2000-06-25","2000-06-26","2000-06-27","2000-06-28","2000-06-29","2000-06-30","2000-07-01","2000-07-02","2000-07-03","2000-07-04","2000-07-05","2000-07-06","2000-07-07","2000-07-08","2000-07-09","2000-07-10","2000-07-11","2000-07-12","2000-07-13","2000-07-14","2000-07-15","2000-07-16","2000-07-17","2000-07-18","2000-07-19","2000-07-20"],"Reservoir Level":["102.95","103.01","103.05","102.93","102.78","102.83","102.78","102.87","102.92","102.97","103.02","103.05","103.29","103.3","103.29","103.15","103","102.92","102.95","102.98","102.9","102.8","102.8","102.81","102.86","102.9","102.92","102.94","102.96","102.98","102.99","102.99","102.3","103","103.02","103.03","103.04","103.05","103.05","103.06","103.1","103.15","103.19","103.19","103.2","103.3","103.32","103.33","103.34","103.19","102.98","102.89","102.89","102.91","102.96","103.05","103.06","103.07","103.27","103.31","103.28","103.12","102.95","102.94","102.97","102.98","103.02","103.02","103.08","103.14","103.18","103.54","103.57","103.48","103.35","103.21","103.12","103.2","103.3","103.38","103.4","103.4","103.4","103.4","103.4","103.34","103.34","103.27","103.27","103.28","103.28","103.3","103.3","103.3","103.3","103.25","103.2","103.2","103.14","103.14","103.1","103.1","103","103","103.25","103.25","103.35","103.35","103.45","103.45","103.5","103.5","103.46","103.46","103.37","103.37","103.26","103.26","103.22","103.22","103.26","103.31","103.31","103.49","103.49","103.34","103.34","103.19","103.19","103.21","103.21","103.27","103.27","103.33","103.37","103.37","103.42","103.42","103.33","103.33","103.26","103.26","103.19","103.19","103.35","103.35","103.48","103.48","103.55","103.55","103.55","103.6","103.46","103.46","103.46","103.33","103.33","103.33","103.62","103.62","103.62","103.62","103.6","103.5","103.5","103.38","103.38","103.24","103.24","103.26","103.26","103.3","103.3","103.2","103.2","103.16","103.23","103.23","103.26","103.26","103.29","103.29","103.31","103.31","103.32","103.32","103.35","103.35","103.37","103.42","103.42","103.3","103.3","103.25","103.25","103.28","103.28","103.3","103.3","103.31","103.31","103.25","103.25","103.18","103.18","103.18","103.18","103.22","103.12","103.13","103.14","103.16","103.23","103.65","103.52","104.03","103.84","103.68","103.59","103.45","103.31","103.2","103.11","103.13","103.15","103.19","103.22","103.25","103.27","103.29","103.3","103.32","103.24","103.13","103.12","103.17","103.21","103.25","103.15","103.04","103.07","103.09","103.1","103.11","103.11","103.11","103.1","103.05","103.08","103.1","103.11","103.11","103.11","103.1","103.1","103.11","103.13"],"Rainfall":[null,"14.5","4.5","7","2.5","54",null,"18","2",null,"1",null,"24.5","9",null,null,null,null,null,"0.5",null,null,null,null,"10.5",null,null,null,null,null,null,null,null,null,null,null,"5",null,null,null,"17.5","7.5",null,null,"7","20",null,null,null,null,null,null,null,"5","10","9.5","0","0","45","0","42","0","0","0","0","2.5","27.5","0","29","39","2","93","6","0","0","14","0","8.5","0","43.5","31.5","0","0","32","32","0","0","2","2","14","14","20","20","4","4","0","0","5.5","5.5","0","0","17.5","17.5","0","0","16","16","0","0","14","14","0","0","0.5","0.5","0","0","0","0","0","0","8","8","3.5","3.5","69.5","69.5","0","0","0","0","39.5","39.5","5","5","9","9","1","1","0","0","6.5","6.5","11.5","11.5","14.5","14.5","0","0","0","0","0","0","0","21.5","21.5","21.5","0","0","0","0","0","0","54.5","54.5","0","0","14.5","14.5","12","12","0","0","0","0","0","0","0","0","0","0","0","0","31.5","31.5","0","0","0","0","0","0","0","0","0","0","0","0","24.5","24.5","0","0","18","18","0","0","12.5","12.5","6.5","6.5","1","1","0","0","0","0","0","0.5","0","0","12.5","1","57.5","0","63","0","0","40","5.5","0","0","0","0","0","0.5","0.5","0","0","0","0","12.5","2.5","0.5","1","27","11","3.5","13","0","4.5","0","0","0","0","0","0","0","0","0","1.5","0","0","0","0","2","9.5"]}

and here I have a develop a simple function to output it as csv

function convert_to_csv($input_array, $output_file_name, $delimiter) {
    $temp_memory = fopen('php://memory', 'w');

    foreach ($input_array as $key => $set) {
        fputcsv($temp_memory, $set, $delimiter, '"');
    }
    fseek($temp_memory, 0);
    header('Content-Type: application/csv');
    header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
    fpassthru($temp_memory);
}

here the layout that i don't want it to be

enter image description here

and this is the layout that I want it to be

enter image description here

as you can see I failed to find a way to put the header row. Appreciate your helps. Thanks


回答1:


You need transposition.

foreach($input_array as $key=>$set){
  foreach($set as $row=>$val){
    $lines[$row][$key] = $val;
  }
}

For input_array like:

{"datetime":["2000-01-01","2001-01-01"], "Resovior":[1,2], "RainFall":[2,2]}

will produce $lines like

$lines[0]: {"datetime": "2000-01-01", "Resovior": 1, "RainFall": 2}
$lines[1]: {"datetime": "2001-01-01", "Resovior": 2, "RainFall": 2}

Keep in mind that every element of $lines refers to one row in your file, and its' elements are the columns.

Then you can put rows like this:

foreach ($lines as $line)
{
  fputcsv($temp_memory,$line,$delimeter,"'");
}

The way to add a header is the same.

$header = array("DateTime", "Revisior", "Rainfall");
fputcsv($temp_memory, $header, $delimeter, "'");



回答2:


Your output file is wrong formatted, must be like that:

DateTime;Reservoir;Rainfall;
date1;res1;rain1;
date2;res2;rain2;
date3;res3;rain3;

but your current file looks like this one:

date1;date2;date3;
res1;res2;res3;
rain1;rain2;rain3;

You must iterate trough array recursively in order to set the correct format.

A possible solution (taken from https://stackoverflow.com/a/6600130/3518053 ) :

$fh = fopen('file.csv', 'w');

// write out the headers
fputcsv($fh, array_keys($data));

// write out the data    
for ($i = 0; $i < count($data['dates']); $i++) {
    $temp = array($data['dates'][$i], $data['type1'][$i], $data['type2'][$i]);
    fputcsv($fh, $temp);
}

Regards



来源:https://stackoverflow.com/questions/28061579/array-to-csv-file-for-vertical-layout-and-failed-to-put-the-header-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!