如何从不带引号的 php 数组导出制表符分隔的 txt 文件

How to export a tab-separated txt file from php array without quotes

我已经使用以下代码为产品 Feed 保存了一个制表符分隔的文件。但是,我提交的提要要求字段不包含在引号中。有没有办法在字段中不带引号的情况下保存此文件。

$feed[]=array('Item','Description','Category');
$feed[]=array('1-1','Words describing item 1, for example.','Top Category > SubCategory1');
$feed[]=array('1-2','Words describing item 2.','Top Category > SubCategory2');

header('Content-type: text/tab-separated-values');
header("Content-Disposition: attachment;filename=bingproductfeed.txt");
$f  =   fopen('php://output', 'a');
foreach ($feed as $fields) {
    //$fields=str_replace('"','',$fields);
    //$fields=trim($fields,'"');
    fputcsv($f, $fields, "\t");
}

//Outputs:
//Item  Description Category
//1-1   "Words describing item 1, for example." "Top Category > SubCategory1"
//1-2   "Words describing item 2."  "Top Category > SubCategory2"

//I need:
//Item  Description Category
//1-1   Words describing item 1, for example.   Top Category > SubCategory1
//1-2   Words describing item 2.    Top Category > SubCategory2

我试过删除引号并用空格替换它们,但没有成功。有没有办法做到这一点,以便我可以无误地提交此 Feed?

基于 PHP manual 我会说你可以省略 fopen 行并直接在页面上直接回显你的输出。

php://output ¶

php://output is a write-only stream that allows you to write to the output buffer mechanism in the same way as print and echo.

所以像这样:

$feed[]=array('Item','Description','Category');
$feed[]=array('1-1','Words describing item 1, for example.','Top Category > SubCategory1');
$feed[]=array('1-2','Words describing item 2.','Top Category > SubCategory2');

header('Content-type: text/tab-separated-values');
header("Content-Disposition: attachment;filename=bingproductfeed.txt");
foreach ($feed as $fields) {
    //$fields=str_replace('"','',$fields);
    //$fields=trim($fields,'"');
    echo implode("\t",$fields);
}