PHP数组到CSV [英] PHP Array to CSV
本文介绍了PHP数组到CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将一个产品数组转换为一个CSV文件,但它似乎不打算去计划。 CSV文件是一个长行,这里是我的代码:
for($ i = 0; $ i $ sql =SELECT * FROM products WHERE id ='$ prods [$ i]。
$ result = $ mysqli-> query($ sql);
$ info = $ result-> fetch_array();
}
$ header ='';
for($ i = 0; $ i< count($ info); $ i ++)
{
$ row = $ info [$ i];
$ line ='';
for($ b = 0; $ b< count($ row); $ b ++)
{
$ value = $ row [$ b]
if((!isset($ value))||($ value ==))
{
$ value =\t;
}
else
{
$ value = str_replace('','',$ value);
$ value =''。 $ value。 '\\'。\t;
}
$ line。= $ value;
}
$ data。 ;
}
$ data = str_replace(\r,,$ data);
if($ data ==)
{
$ data =\\\
(0)Records Found!\\\
;
}
header(Content-type:application / octet-stream);
header(Content-Disposition:attachment; filename = your_desired_name.xls);
header(Pragma:no-cache);
header(Expires:0);
array_to_CSV($ data);
function array_to_CSV($ data)
{
$ outstream = fopen(php://输出,'r +');
fputcsv($ outstream,$ data,',','');
rewind($ outstream);
$ csv = fgets($ outstream);
fclose($ outstream);
return $ csv;
}
此外,标题不强制下载。我已经复制并粘贴输出并保存为.csv
EDIT
问题解决: p>
如果任何人正在寻找同样的东西,找到一个更好的方法:
$ num = 0;
$ sql =SELECT id,name,description FROM products;
if($ result = $ mysqli-> query($ sql)){
while($ p = $ result-> fetch_array()){
$ prod [$ num] ['id'] = $ p ['id'];
$ prod [$ num] ['name'] = $ p ['name'];
$ prod [$ num] ['description'] = $ p ['description'];
$ num ++;
}
}
$ output = fopen(php:// output,'w')或die(Can not open php:// output);
header(Content-Type:application / csv);
header(Content-Disposition:attachment; filename = pressurecsv.csv);
fputcsv($ output,array('id','name','description'));
foreach($ prod as $ product){
fputcsv($ output,$ product);
}
fclose($ output)或die(Can not close php:// output);
解决方案
http://php.net/manual/en/function.fputcsv.php =noreferrer> fputcsv()
。
这可能会立即解决您的问题。
I'm trying to convert an array of products into a CSV file, but it doesn't seem to be going to plan. The CSV file is one long line, here is my code:
for($i=0;$i<count($prods);$i++) {
$sql = "SELECT * FROM products WHERE id = '".$prods[$i]."'";
$result = $mysqli->query($sql);
$info = $result->fetch_array();
}
$header = '';
for($i=0;$i<count($info);$i++)
{
$row = $info[$i];
$line = '';
for($b=0;$b<count($row);$b++)
{
$value = $row[$b];
if ( ( !isset( $value ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );
if ( $data == "" )
{
$data = "\n(0) Records Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=your_desired_name.xls");
header("Pragma: no-cache");
header("Expires: 0");
array_to_CSV($data);
function array_to_CSV($data)
{
$outstream = fopen("php://output", 'r+');
fputcsv($outstream, $data, ',', '"');
rewind($outstream);
$csv = fgets($outstream);
fclose($outstream);
return $csv;
}
Also, the header doesn't force a download. I've been copy and pasting the output and saving as .csv
EDIT
PROBLEM RESOLVED:
If anyone else was looking for the same thing, found a better way of doing it:
$num = 0;
$sql = "SELECT id, name, description FROM products";
if($result = $mysqli->query($sql)) {
while($p = $result->fetch_array()) {
$prod[$num]['id'] = $p['id'];
$prod[$num]['name'] = $p['name'];
$prod[$num]['description'] = $p['description'];
$num++;
}
}
$output = fopen("php://output",'w') or die("Can't open php://output");
header("Content-Type:application/csv");
header("Content-Disposition:attachment;filename=pressurecsv.csv");
fputcsv($output, array('id','name','description'));
foreach($prod as $product) {
fputcsv($output, $product);
}
fclose($output) or die("Can't close php://output");
解决方案
Instead of writing out values consider using fputcsv()
.
This may solve your problem immediately.
这篇关于PHP数组到CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文