垂直布局的 CSV 文件数组,未能放置 header 行

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

我在为 csv 创建水平布局时遇到问题

这是一个数组,我已将其转换为 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"]}

这里我开发了一个简单的函数来将其输出为 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);
}

这是我不希望的布局

这是我想要的布局

如您所见,我未能找到放置 header 行的方法。感谢您的帮助。谢谢

您的输出文件格式错误,必须是这样的:

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

但是您当前的文件看起来像这样:

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

您必须递归地遍历数组以设置正确的格式。

可能的解决方案(取自):

$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);
}

此致

你需要换位。

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

对于input_array喜欢:

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

会产生 $lines 喜欢

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

请记住,$lines 的每个元素都引用文件中的一个 ,它的元素是 .

然后你可以像这样放置行:

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

加一个header的方法是一样的。

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