简体   繁体   English

如何使用 php fputcsv 在 csv 中保留尾随零?

[英]How to keep trailing zeros in csv using php fputcsv?

I am using fputcsv function to write data in a file.But it is removing the trailing zeros from the output. Here is the code I am using我正在使用 fputcsv function 在文件中写入数据。但它正在从 output 中删除尾随零。这是我正在使用的代码

foreach($invoice_hdr_array as $csv_response) 
{
    fputcsv($fh1, $csv_response, ',', '"');  
}

If you observe the end of the line it should be 0.00.如果您观察该行的末尾,它应该是 0.00。 But I am getting as only 0. In out put I am getting like this LIN,1,1234567890123,EN,,,1.00,94.00,94.00,0但我得到的只有 0。在输出中我得到这样的LIN,1,1234567890123,EN,,,1.00,94.00,94.00,0

But this should be like below但这应该像下面这样

LIN,1,1234567890123,EN,,,1.00,94.00,94.00,0.00

Any help would be greatly appreciated.任何帮助将不胜感激。 Thank you.谢谢你。

If I test your input, and leave numbers as numbers, I get:如果我测试您的输入,并将数字保留为数字,我会得到:

LIN,1,1234567890123,EN,,,1,94,94,0

Observe how all whole numbers loose their zero's, which is not what you get.观察所有整数如何失去它们的零,这不是你得到的。 Your question seems inaccurate?你的问题似乎不准确? The only reason, I can think of, why you get 94.00 , is when it is a string.我能想到的唯一原因是你得到94.00的原因是它是一个字符串。 To illustrate this point I created this code:为了说明这一点,我创建了这段代码:

$array = ['LIN', 1,1234567890123, 'EN', '', '', 1.00, '94.00', 94.00, '0.00'];
$out = fopen('php://output', 'w');
fputcsv($out, $array);
fclose($out);

And the output is:而 output 是:

LIN,1,1234567890123,EN,,,1,94.00,94,0.00 

So I see 2 solutions:所以我看到 2 个解决方案:

  1. Make numbers with trailing zero's into strings.将尾随零的数字变成字符串。
  2. Don't use fputcsv() , but make your own version.不要使用fputcsv() ,而是制作您自己的版本。

And finally, do I need to point out that 0 is, numerically, the same as 0.00 ?最后,我是否需要指出0在数字上与0.00相同?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM