PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Nov 19, 2018 8:07 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed Apr 18, 2018 12:33 pm 
Offline
Forum Newbie

Joined: Tue Apr 10, 2018 12:21 pm
Posts: 2
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


Top
 Profile  
 
PostPosted: Wed Apr 18, 2018 10:22 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13583
Location: New York, NY, US
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)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot], Exabot [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group