selvakumar Posted November 21, 2020 Share Posted November 21, 2020 Team I tried to create csv output ( every value in double quotes) in my php using below function if($generate_csv){ generateCSV($custom[0]['details'], date('Y_m_d_')."check".".CSV", $detail_headers); } function generateCSV($data, $filename) { $file = fopen("/local/myloc/populated/".$filename,"w+"); $i = 0; foreach ($data as $line) { $fields = array(); foreach ($line as $k => $v) { $fields[$k] = $v; } if( $i === 0 ) { fputcsv($file, array_keys($fields) ); // First write the headers } fputcsv($file, array_values($fields)); $i++; } fclose($file); } I got below output with one issue (There is a blank at the end of each value) Date,time,Device,lable,throughput_read,throughput_write "11/19/2020 ","00:00 ","audigysg36s6101 ","host17 ","0,00 ","0,00 " "11/19/2020 ","00:00 ","audigysg36s6101 ","host1 ","0,00 ","0,00 " "11/19/2020 ","00:00 ","audigysg36s6101 ","host18 ","0,00 ","0,00 " Could you please help me to resolve this issue Quote Link to comment https://forums.phpfreaks.com/topic/311752-csv-output-using-php/ Share on other sites More sharing options...
gw1500se Posted November 21, 2020 Share Posted November 21, 2020 First please use the code icon (<>) for your code and specify PHP. As for your specific problem, I don't know how those variables are generated (that is where the space is coming from) but simply using rtrim should solve the problem. $fields[$k] = rtrim($v); Quote Link to comment https://forums.phpfreaks.com/topic/311752-csv-output-using-php/#findComment-1582562 Share on other sites More sharing options...
selvakumar Posted November 22, 2020 Author Share Posted November 22, 2020 (edited) Thank you very much gw1500se . I somewhat messed up the question. Actually i want to create a file on CSV format. Below is my logic to achieve my goal if($generate_csv){ generateCSV($custom[0]['details'], date('Y_m_d_')."check".".CSV", $detail_headers); } function generateCSV($data, $filename) { $file = fopen("/local/myloc/populated/".$filename,"w+"); $i = 0; foreach ($data as $line) { $fields = array(); foreach ($line as $k => $v) { $fields[$k] = $v; } if( $i === 0 ) { fputcsv($file, array_keys($fields) ); // First write the headers } fputcsv($file, array_values($fields)); $i++; } fclose($file); } There is no issue in my output Date,time,Device,lable,throughput_read,throughput_write 11/21/2020,00:00,audigysg36s6101,192905199.0,1742118968.0,11.073020990149,97.584 11/21/2020,00:05,audigysg36s6101,192914200.0,1742253920.0,11.072679922568,97.559 But i need to show every value in double quotes like "11/21/2020,00:00","audigysg36s6101","192905199.0","1742118968.0","11.073020990149","97.584" to get above output, i changed my code $fields[$k] = $v. ' '; Now i got one blank space in at the end of each value like "11/21/2020,00:00 ","audigysg36s6101 ","192905199.0 ","1742118968.0 ","11.073020990149 ","97.584 " If i use trim then it removes all double quotes in the line that i don't need. 11/21/2020,00:00,audigysg36s6101,192905199.0,1742118968.0,11.073020990149,97.584 Input values to get csv [1] => Array ( [Date] => 11/21/2020 [time] => 00:00 [Device] => audigysg36s6101 [lable] => 192905199.0 [throughput_read] => 1742118968.0 [throughput_write] => 11.073020990149 ) [1] => Array ( [Date] => 11/21/2020 [time] => 00:15 [Device] => audigysg36s6101 [lable] => 192914200.0 [throughput_read] => 1742253920.0 [throughput_write] => 11.072679922568 ) Edited November 22, 2020 by Barand code tags added Quote Link to comment https://forums.phpfreaks.com/topic/311752-csv-output-using-php/#findComment-1582600 Share on other sites More sharing options...
gw1500se Posted November 22, 2020 Share Posted November 22, 2020 $fields[$k] = "\"".$v"\""; Quote Link to comment https://forums.phpfreaks.com/topic/311752-csv-output-using-php/#findComment-1582604 Share on other sites More sharing options...
kicken Posted November 22, 2020 Share Posted November 22, 2020 I don't think fputcsv allows you to force quotes around values. Generally speaking quotes should not be necessary except under specific circumstances. Is there any particular reason you need to have every value quoted? If you do need every value quoted, you'll have to generate the output yourself. Quote Link to comment https://forums.phpfreaks.com/topic/311752-csv-output-using-php/#findComment-1582605 Share on other sites More sharing options...
Barand Posted November 22, 2020 Share Posted November 22, 2020 (edited) To expand on what @kicken said, numeric values do not get quoted and string values only get quoted if they contain one or more commas. If you really do need everything quoted then you could... function generateCSV($data, $filename) { $fp = fopen($filename, 'w'); foreach ($data as $k => $v) { if ($k==0) { fputcsv($fp, array_keys($v)); } fprintf($fp, '"%s","%s","%s","%s","%s","%s"'."\n", ...array_values($v)); } fclose($fp); } OUTPUT: Date,time,Device,label,throughput_read,throughput_write "11/21/2020","00:00","audigysg36s6101","192905199","1742118968","11.073020990149" "11/21/2020","00:15","audigysg36s6101","192914200","1742253920","11.072679922568" Edited November 22, 2020 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/311752-csv-output-using-php/#findComment-1582606 Share on other sites More sharing options...
selvakumar Posted November 22, 2020 Author Share Posted November 22, 2020 (edited) Thanks gw1500se/Barand @gw1500se $fields[$k] = "\"".$v."\""; It doesn't provide desired result Date,time,Device,label,throughput_read,throughput_write """11/21/2020""","""00:00""","""audigysg36s6101""","""192905199.0""","""1742118968.0""","""11.073020990149""","""97.584""" """11/21/2020""","""00:05""","""audigysg36s6101""","""192914200.0""","""1742253920.0""","""11.072679922568""","""97.559""" @Barand I tried to apply your logic, but no luck . I am not sure why it failed to me function generateCSV($data, $filename) { $fp = = fopen("/local/myloc/populated/".$filename,"w+"); foreach ($data as $k => $v) { if( $k === 0 ) { fputcsv($fp, array_keys($v)); // First write the headers } fprintf($fp, '"%s","%s","%s","%s","%s","%s"'."\n",array_keys($v)); } fclose($fp); } head -20 -v 2020_11_22_SERVER.CSV ==> 2020_11_22_SERVER.CSV <== Date,time,Device,label,throughput_read,throughput_write No Rows Edited November 22, 2020 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/311752-csv-output-using-php/#findComment-1582612 Share on other sites More sharing options...
Barand Posted November 22, 2020 Share Posted November 22, 2020 That is not the same as the code I posted and should be generating several errors - if you have your error display/reporting turned on. Quote Link to comment https://forums.phpfreaks.com/topic/311752-csv-output-using-php/#findComment-1582613 Share on other sites More sharing options...
gw1500se Posted November 22, 2020 Share Posted November 22, 2020 This comes back to the earlier question. Why do you need the quotes in the first place? Quote Link to comment https://forums.phpfreaks.com/topic/311752-csv-output-using-php/#findComment-1582614 Share on other sites More sharing options...
selvakumar Posted November 22, 2020 Author Share Posted November 22, 2020 @gw1500se Yes. I want. I really don't know why my tester insist this. There is no issue in values. Only in format double quotation Quote Link to comment https://forums.phpfreaks.com/topic/311752-csv-output-using-php/#findComment-1582615 Share on other sites More sharing options...
gw1500se Posted November 22, 2020 Share Posted November 22, 2020 Barand's code will do it. Quote Link to comment https://forums.phpfreaks.com/topic/311752-csv-output-using-php/#findComment-1582616 Share on other sites More sharing options...
selvakumar Posted November 22, 2020 Author Share Posted November 22, 2020 @Barand I am little bit curious that you shown the desired result. But when I used your logic in my code. It failed function generateCSV($data, $filename) { $fp = fopen("/local/myloc/populated/".$filename,"w+"); foreach ($data as $k => $v) { if ($k==0) { fputcsv($fp, array_keys($v)); } fprintf($fp, '"%s","%s","%s","%s","%s","%s"'."\n",array_values($v)); } fclose($fp); } I made only 2 changes. 1) path .. i don't think any issue 2) removed 3 dots before array_vales. If i keep that one, my report fails to run. Overall my report runs without any error. Logs generated. But CSV failed to generate Quote Link to comment https://forums.phpfreaks.com/topic/311752-csv-output-using-php/#findComment-1582617 Share on other sites More sharing options...
Barand Posted November 22, 2020 Share Posted November 22, 2020 The three dots are significant, expanding the array into a comma separated list of values. They won't work with a PHP version before v5.6, but if that's case then you have more work to do bringing your PHP up to date (v7.4). Have you turned your error reporting and display on yet? Quote Link to comment https://forums.phpfreaks.com/topic/311752-csv-output-using-php/#findComment-1582618 Share on other sites More sharing options...
Barand Posted November 23, 2020 Share Posted November 23, 2020 If the "ellipsis" operator is still a problem for you, that line can be rewritten as fwrite($fp, vsprintf('"%s","%s","%s","%s","%s","%s"'."\n", $v)); Quote Link to comment https://forums.phpfreaks.com/topic/311752-csv-output-using-php/#findComment-1582625 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.