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条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大