使用PHP将列标题添加到CSV文件 [英] Add Column Headers To a CSV File With PHP

查看:422
本文介绍了使用PHP将列标题添加到CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将php / mysql生成的表转换为可下载的csv文件。当用户输入搜索参数时,会调用mysql表,并返回表格结果。



我使用了这个线程提供的解决方案, a href =http://stackoverflow.com/questions/217424/create-a-csv-file-for-a-user-in-php>为PHP用户创建CSV文件 p>

我可以允许用户以csv文件保存或查看结果。我的问题是我不知道如何添加列标题到文件。例如,返回的结果格式如下:$ result [x] ['office'],$ result [x] ['user']等等,但我也想添加列标题Office和User,以便任何人查看csv文件立即知道数据的含义。



这是生成csv文件的函数:

  header(Content-type:text / csv); 
header(Content-Disposition:attachment; filename = $ file.csv);
header(Pragma:no-cache);
header(Expires:0);
outputCSV($ data);

function outputCSV($ data){
$ outstream = fopen(php:// output,a);
function __outputCSV(& $ vals,$ key,$ filehandler){
fputcsv($ filehandler,$ vals); //添加参数,如果你想
}
array_walk($ data,__outputCSV,$ outstream);
fclose($ outstream);
}



我尝试创建以下函数,然后在调用之前调用outputCSV,但它不成功:

  function create_file_header()
{
$ headers ='Office ,User,Tag,Value';
$ fp = fopen(php:// output,w);
file_put_contents($ file.csv,$ headers);
fclose($ fp);
}

任何帮助都非常感激。

fputcsv 正常。



你不谈论 $ data 的格式,但如果它是一个关联数组,你可以做一个通用的解决方案:

 函数toCSV($ data,$ outstream){
if(count $ data)){
//从键
获取标题fputcsv($ outstream,array_keys($ data [0]));
//
foreach($ data as $ row){
fputcsv($ outstream,$ row)
}
}
}

请参阅 array_walk() foreach 循环的好处。


I am trying to convert a php/mysql generated table into a downloadable csv file. When a user enters search parameters, a call is made to a mysql table and results returned as table.

I used the solution offered in this thread and it works fabulously: Create a CSV File for a user in PHP

I am able to allow users to save or view the results as a csv file. the problem I am having is I'm not sure how to add column headers to the file. For example, the results that are returned are in the following format: $result[x]['office'], $result[x]['user'], etc..., but I also want to add column titles like "Office" and "User" so that anyone looking at the csv file immediately knows what the data means.

This is the function that generates the csv file:

    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename=$file.csv");
    header("Pragma: no-cache");
    header("Expires: 0");
    outputCSV($data);

function outputCSV($data) {
        $outstream = fopen("php://output", "a");
        function __outputCSV(&$vals, $key, $filehandler) {
            fputcsv($filehandler, $vals); // add parameters if you want
        }
        array_walk($data, "__outputCSV", $outstream);
        fclose($outstream);
    }

I have tried creating the following function, and then calling it right before I call outputCSV, but it is not successful:

function create_file_header()
{       
    $headers = 'Office, User, Tag, Value';
    $fp = fopen("php://output", "w");
    file_put_contents("$file.csv", $headers);
    fclose($fp);
}

Any help is greatly appreciated.

解决方案

To create headers, you should just prepend a header row array and use fputcsv as normal.

You don't talk about the format of $data, but if it's an associative array you can do this for a generic solution:

function toCSV($data, $outstream) {
    if (count($data)) {
        // get header from keys
        fputcsv($outstream, array_keys($data[0]));
        // 
        foreach ($data as $row) {
            fputcsv($outstream, $row);
        }
    }
}

I'm don't see the benefit of array_walk() over a foreach loop.

这篇关于使用PHP将列标题添加到CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆