如何将数组导出到csv

时间:2014-09-09 08:20:58

标签: php arrays 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"]

我想将此数组转换为csv导出文件。我试过这样的代码。

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

?>

但我无法获取数据。任何身体都可以帮助我。我尝试了很多方法。我认为我的阵列格式给人带来麻烦。任何人都可以从中推荐数组格式,以便轻松导出。

2 个答案:

答案 0 :(得分:1)

您将一串连接值作为行传递给fputcsv(),其中每个列都需要一个数组元素。您的数据应分成每行的列值数组,例如I.E:

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

更改此阵列是您要解决此问题的方法。如果您无法更改数组的创建方式,则可以使用fputcsv()始终重新映射数据,使其符合array_map()期望的格式:

$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

答案 1 :(得分:0)

这是一个工作解决方案的示例,也许您可​​以将其作为基础:

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

循环遍历数组的所有元素,将值分隔; s,当达到最终元素时,它会放置一个新的行分隔符。

一旦将其连接成一个整体,file_put_contents就会创建一个CSV文件。