PHP 分号分隔的文件生成
PHP semicolon delimited file generation
我正在使用此代码生成分号分隔文件:
for ($i = 0; $i < 3; $i = $i+1)
{
array_push($dataArray,"$dataCell1","$dataCell2","$dataCell3","$dataCell4","$dataCell5","$dataCell6","$dataCell7","$dataCell8","$dataCell9","$dataCell10","$dataCell11");
$stringData = rtrim(implode(';', $dataArray), ';'); //rtrim will prevent the last cell to be imploded by ';'
$stringData .= "\r\n";
}
我想要的是:
(数据以分号分隔,行以换行符分隔)
我得到的是:
(数据以分号分隔但不加新行,所有数据都出现在一行中)
请告诉我我做错了什么..
改用fputcsv
:
// indexed array of data
$data = [
['col1' => 1, 'col2' => 2, 'col3' => 3],
['col1' => 6, 'col2' => 7, 'col3' => 9],
['col1' => 5, 'col2' => 8, 'col3' => 15],
]
// add headers for each column in the CSV download
array_unshift($data, array_keys(reset($data)));
$handle = fopen('php://output', 'w');
foreach ($data as $row) {
fputcsv($handle, $row, ';', '"');
}
fclose($handle);
对于创建csv文件,fputcsv
确实是最好的方法。
首先将您的数据保存在 "array of arrays" 中。这使得处理数据更容易:
$dataArray = array();
for ($i = 0; $i < 3; $i = $i+1)
$dataArray[$i] = array($dataCell1,$dataCell2,$dataCell3,$dataCell4,$dataCell5,$dataCell6,$dataCell7,$dataCell8,$dataCell9,$dataCell10,$dataCell11);
接下来我们需要创建一个临时文件的句柄,同时还要确保我们有权这样做。然后我们将使用正确的分隔符将所有数组条目存储在临时文件中。
$handle = fopen('php://temp', 'r+');
foreach($dataArray as $line)
fputcsv($handle, $line, ';', '"');
rewind($handle);
如果您只是想将结果打印到页面上,可以使用以下代码:
$contents = "";
while(!feof($handle))
$contents .= fread($handle, 8192);
fclose($handle);
echo $contents;
请注意,在普通 html 中,不会显示换行符。但是,如果您检查结果页面的源代码,您会看到换行符。
但如果您还想将这些值保存在可下载的 csv 文件中,则需要使用以下代码:
$fileName = "file.csv";
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $fileName . '";');
fpassthru($handle);
fclose($handle);
你的问题出在你的逻辑上。您在每次迭代中继续向 $dataArray 添加数据,而代码中的以下语句会将 stingData 设置为 $dataArray 的内爆值 仅在最后一次循环迭代中。那时,$dataArray 包含所有数据值,因为您一直向它推送 3 次迭代。
$stringData = rtrim(...)
你要做的是:
<?php
//concatenation string
$stringData = '';
for ($i = 0; $i < 3; $i = $i+1)
{
//init the array with every iteration.
$dataArray = array();
//push your values
array_push($dataArray,"$dataCell1","$dataCell2","$dataCell3","$dataCell4","$dataCell5","$dataCell6","$dataCell7","$dataCell8","$dataCell9","$dataCell10","$dataCell11");
//concatenate
$stringData .= rtrim(implode(';', $dataArray), ';');
//new line
$stringData .= "\r\n";
}
//print
echo $stringData . "\n";
?>
我正在使用此代码生成分号分隔文件:
for ($i = 0; $i < 3; $i = $i+1)
{
array_push($dataArray,"$dataCell1","$dataCell2","$dataCell3","$dataCell4","$dataCell5","$dataCell6","$dataCell7","$dataCell8","$dataCell9","$dataCell10","$dataCell11");
$stringData = rtrim(implode(';', $dataArray), ';'); //rtrim will prevent the last cell to be imploded by ';'
$stringData .= "\r\n";
}
我想要的是:
(数据以分号分隔,行以换行符分隔)
我得到的是:
请告诉我我做错了什么..
改用fputcsv
:
// indexed array of data
$data = [
['col1' => 1, 'col2' => 2, 'col3' => 3],
['col1' => 6, 'col2' => 7, 'col3' => 9],
['col1' => 5, 'col2' => 8, 'col3' => 15],
]
// add headers for each column in the CSV download
array_unshift($data, array_keys(reset($data)));
$handle = fopen('php://output', 'w');
foreach ($data as $row) {
fputcsv($handle, $row, ';', '"');
}
fclose($handle);
对于创建csv文件,fputcsv
确实是最好的方法。
首先将您的数据保存在 "array of arrays" 中。这使得处理数据更容易:
$dataArray = array();
for ($i = 0; $i < 3; $i = $i+1)
$dataArray[$i] = array($dataCell1,$dataCell2,$dataCell3,$dataCell4,$dataCell5,$dataCell6,$dataCell7,$dataCell8,$dataCell9,$dataCell10,$dataCell11);
接下来我们需要创建一个临时文件的句柄,同时还要确保我们有权这样做。然后我们将使用正确的分隔符将所有数组条目存储在临时文件中。
$handle = fopen('php://temp', 'r+');
foreach($dataArray as $line)
fputcsv($handle, $line, ';', '"');
rewind($handle);
如果您只是想将结果打印到页面上,可以使用以下代码:
$contents = "";
while(!feof($handle))
$contents .= fread($handle, 8192);
fclose($handle);
echo $contents;
请注意,在普通 html 中,不会显示换行符。但是,如果您检查结果页面的源代码,您会看到换行符。
但如果您还想将这些值保存在可下载的 csv 文件中,则需要使用以下代码:
$fileName = "file.csv";
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $fileName . '";');
fpassthru($handle);
fclose($handle);
你的问题出在你的逻辑上。您在每次迭代中继续向 $dataArray 添加数据,而代码中的以下语句会将 stingData 设置为 $dataArray 的内爆值 仅在最后一次循环迭代中。那时,$dataArray 包含所有数据值,因为您一直向它推送 3 次迭代。
$stringData = rtrim(...)
你要做的是:
<?php
//concatenation string
$stringData = '';
for ($i = 0; $i < 3; $i = $i+1)
{
//init the array with every iteration.
$dataArray = array();
//push your values
array_push($dataArray,"$dataCell1","$dataCell2","$dataCell3","$dataCell4","$dataCell5","$dataCell6","$dataCell7","$dataCell8","$dataCell9","$dataCell10","$dataCell11");
//concatenate
$stringData .= rtrim(implode(';', $dataArray), ';');
//new line
$stringData .= "\r\n";
}
//print
echo $stringData . "\n";
?>