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: Select all
foreach ($data as $fields) {
fputcsv($fp, $fields);
}
Code: Select all
$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]);
Or
Replace the datetime as an single array value into the existing array