Hello All,
I have an array which needs to be converted into CSV format.
2D Array is as below:
I want the 0th element to be replaced by DateTime like
Array
(
[0] => Array
(
[0] => 1 >>Should be replaced by 20170922,09:20:00
[1] => 31.85
[2] => 32.15
[3] => 31.75
[4] => 32.15
[5] => 401352
)
[1] => Array
(
[0] => 2 >> Should be replaced by 20170922,09:25:00
[1] => 31.85
[2] => 32
[3] => 31.75
[4] => 31.85
[5] => 249158
)
)
Since DateTime is a string and this is an array while writing to the file it takes double quotes ( " ) for the string and the output becomes
"20170922,09:20:00",678.6,682.8,677.2,681.75,159294 >> Invalid CSV
I use fputcsv to write the data
Code:
foreach ($data as $fields) {
fputcsv($fp, $fields);
}
My code, merges array data with string based date
Code:
$data[$i][0]=date('Ymd,H:i:s', $data[0][0] + ($data[$i][0]*300));
$data[0][0]=date('Ymd,H:i:s', $timestamp[0]);
Could you please suggest How could I remove these quotes from the csv
Or
Replace the datetime as an single array value into the existing array
String to Array For CSV Writing
Moderator: General Moderators
-
geralde.givens
- Forum Newbie
- Posts: 2
- Joined: Tue Apr 10, 2018 12:21 pm
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: String to Array For CSV Writing
You can get rid of the quotes the way your are doing it because each array element is a column in the CSV row. Use array_unshift() to add the date onto the front of each row array. Then set element [1] to the time.
(#10850)