Welcome to WebmasterWorld Guest from 54.226.159.223

Forum Moderators: coopster & jatar k

String to Array For CSV Writing

String, Array, CSV, Double Quotes

     
3:20 am on Sep 23, 2017 (gmt 0)

Full Member

10+ Year Member

joined:Aug 13, 2007
posts: 218
votes: 0


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


foreach ($data as $fields) {
fputcsv($fp, $fields);
}


My code, merges array data with string based date


$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
9:39 am on Sept 23, 2017 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 306
votes: 21


"20170922,09:20:00",678.6,682.8,677.2,681.75,159294 >> Invalid CSV


Well, that's arguably valid CSV. (Any CSV parser should read that back OK.) If you simply removed the quotes then you'd end up with two fields, where previously there was only one. One for the date and one for the time (since you are using a comma in the format string to separate the two) - is that the intention?

Or, store the DateTime as a numeric timestamp (preferable) and convert the DateTime back into a human readable form when you read the data back in (or when you display it).

[edited by: whitespace at 10:01 am (utc) on Sep 23, 2017]

9:54 am on Sept 23, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member tangor is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Nov 29, 2005
posts:7994
votes: 578


The "or" is +1 :)
10:30 am on Sept 23, 2017 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 20, 2017
posts:301
votes: 57


date('Ymd,H:i:s', $timestamp[0]);

Is there a particular reason you need to store the date in this format? (with a coma to separate the day from the time?

If you need a representation of the time which is humanely readable (at the difference of a timestamp), you should use the DATE_RFC3339, or DATE_W3C format.

date ( DATE_RFC3339 , $timestamp [ 0 ] ) ;
date ( DATE_W3C , $timestamp [ 0 ] ) ;

which should give something like
2017-09-22T09:20:00+00:00
12:13 pm on Sept 23, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11156
votes: 116


"20170922,09:20:00",678.6,682.8,677.2,681.75,159294 >> Invalid CSV

this is valid and required.
since your DateTIme format contains an embedded comma it is necessary to escape that field with double quotes since the comma is also your field separator.

see IETF's RFC 4180
Common Format and MIME Type for Comma-Separated Values (CSV) Files

The ABNF grammar appears as follows:
file = [header CRLF] record *(CRLF record) [CRLF]
header = name *(COMMA name)
record = field *(COMMA field)
name = field
field = (escaped / non-escaped)
escaped = DQUOTE *(TEXTDATA / COMMA / CR / LF / 2DQUOTE) DQUOTE
non-escaped = *TEXTDATA

COMMA = %x2C
CR = %x0D ;as per section 6.1 of RFC 2234
DQUOTE = %x22 ;as per section 6.1 of RFC 2234
LF = %x0A ;as per section 6.1 of RFC 2234
CRLF = CR LF ;as per section 6.1 of RFC 2234
TEXTDATA = %x20-21 / %x23-2B / %x2D-7E

https://www.ietf.org/rfc/rfc4180.txt [ietf.org]