drtsd7864 2010-11-01 19:46
浏览 26
已采纳

如何在php语句中引用值? 周一大脑冻结

I have a piece of php code that reads values from a database resultset, shoves them into an array, and writes them to a CSV file. I need to force each value to be surrounded by double quotes when it goes into the CSV file, and can't for the life of me remember how to do this.

Here's the loop that writes to the file (this is actually contained in another loop to go through each row; this loop I'm showing goes through the columns in the currently-selected row):

foreach ($row as $key => $val){

    $newrow[$key] = preg_replace("(
|
||\t)", "", $val);

}

fputcsv($fp, $newrow);

What I need is for the value of $val to be enclosed in double quotes when it's written to the file. Right now, the results I get look like this:

554,702180,25.00,6FYAK0,9090909090909090

What I need is this:

"554","702180","25.00","6FYAK0","9090909090909090"

Can someone jump start my brain?

  • 写回答

5条回答 默认 最新

  • duanliang1019 2010-11-01 21:51
    关注

    I would use substitute function for fputscsv:

    function my_fputcsv($handle, $fieldsarray, $delimiter = ",", $enclosure ='"'){
        $glue = $enclosure . $delimiter . $enclosure;
        return fwrite($handle, $enclosure . implode($glue,$fieldsarray) . $enclosure);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!