简体   繁体   English

如何将数组导出到csv

[英]How to export array into csv

["Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:22, GENDER:MALE, Age:63,Bp_Systolic:120,Bp_Diastolic:80", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:27, GENDER:FEMALE, Age:56,Bp_Systolic:110,Bp_Diastolic:70", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:33, GENDER:MALE, Age:58,Bp_Systolic:130,Bp_Diastolic:70", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:48, GENDER:FEMALE, Age:62,Bp_Systolic:132,Bp_Diastolic:80", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:50, GENDER:MALE, Age:60,Bp_Systolic:120,Bp_Diastolic:70", "Boxname:MH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:51, GENDER:MALE, Age:63,Bp_Systolic:118,Bp_Diastolic:62"]

I want to convert this array into csv export file.我想将此数组转换为 csv 导出文件。 I have tried code like this.我试过这样的代码。

<?php
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// output the column headings
fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));

$list=$name;
foreach ($list as $line)
  {
fputcsv($output, $line);
  }

?>

But i can t able to get the data.但我无法获取数据。 Can any body help me out.任何机构都可以帮助我。 I have tried many ways.我尝试了很多方法。 I think my array format giving trouble.我认为我的数组格式有问题。 Can anybody suggest array format from this to make the export easy.任何人都可以从中建议数组格式以使导出变得容易。

You are passing a string of concatenated values as rows to fputcsv() where it expects an array element for each column.您将一串串联值作为行传递给fputcsv() ,其中每列都有一个数组元素。 Your data should be split into an array of column values for each row, IE:您的数据应拆分为每行的列值数组,即:

$list = Array(
    Array("Boxname:HH", "X1:53.3", "X2:106.6", "Y1:33.300000000000004", "Y2:59.947215189873425", "PID:22", "GENDER:MALE", "Age:63", "Bp_Systolic:120", "Bp_Diastolic:80"),
    Array("Boxname:HH", "X1:53.3", ..)
    //etc
)

Changing this array is how you are going to fix this.更改此数组是您将如何解决此问题。 If you can't change the way the array is created, you can always remap it to have it in the format fputcsv() expects, using array_map() :如果您无法更改数组的创建方式,您始终可以使用array_map()重新映射它以使其具有fputcsv()期望的格式:

$list = ["Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:22, GENDER:MALE, Age:63,Bp_Systolic:120,Bp_Diastolic:80", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:27, GENDER:FEMALE, Age:56,Bp_Systolic:110,Bp_Diastolic:70", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:33, GENDER:MALE, Age:58,Bp_Systolic:130,Bp_Diastolic:70", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:48, GENDER:FEMALE, Age:62,Bp_Systolic:132,Bp_Diastolic:80", "Boxname:HH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:50, GENDER:MALE, Age:60,Bp_Systolic:120,Bp_Diastolic:70", "Boxname:MH, X1:53.3, X2:106.6, Y1:33.300000000000004,Y2:59.947215189873425, PID:51, GENDER:MALE, Age:63,Bp_Systolic:118,Bp_Diastolic:62"];

$list = array_map(function($i){
    return array_map("trim", // Trim the values
        explode(",", $i) // Explode each row into an array of column values
    );
}, $list);

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');

$output = fopen('php://output', 'w');

fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));

foreach ($list as $line) {
    fputcsv($output, $line);
}

DEMO演示

This is an example of a working solution, maybe you could use this as a base:这是一个工作解决方案的示例,也许您可​​以将其用作基础:

$maxSize = count($commentArray);

    for ($c=0; $c < $maxSize; $c++) {
        $toCSV .= $nameArray[$c] . ";" . $amountArray[$c] . ";" . $dateArray[$c] . ";" . $commentArray[$c] . ";" . $bankaccount[$c] . "\n";
    }

file_put_contents($file, $toCSV);       

It loops through all elements of the arrays, separates the values by ;它遍历数组的所有元素,用;分隔值; s and when the final element is reached then it puts a new line separator. s,当到达最后一个元素时,它会放置一个新的行分隔符。

Once this is concatenated as a whole, file_put_contents creates a CSV file.将其连接为一个整体后,file_put_contents 将创建一个 CSV 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM