douzhang5984 2014-05-07 19:08 采纳率: 100%
浏览 85
已采纳

在PHP中格式化数组,使用fputcsv输出到csv

I have an array in this format:

array

That I want to output to csv. The trouble I'm having is getting that array in the right format. When I do:

foreach ($array as $row) {
   fputcsv($df, $row);
}

I only get the integer values. I am missing a column with the names like ACCESSORIES.

Here is the output:

csv

But I want column A to have the names like ACCESSORIES and column B to have the values.

  • 写回答

3条回答 默认 最新

  • donglv1831 2014-05-07 19:12
    关注

    fputcsv just outputs the array values; the keys would usually be column names, not additional columns in the row. You need to make an array out of each element's key and value.

    foreach ($array as $row) {
        $keys = array_keys($row);
        fputcsv($df, array($keys[0], $row[$keys[0]]));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?