Forum Moderators: coopster

Message Too Old, No Replies

String to Array For CSV Writing

String, Array, CSV, Double Quotes

         

kkonline

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

10+ Year Member



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

whitespace

9:39 am on Sep 23, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



"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]

tangor

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

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The "or" is +1 :)

Peter_S

10:30 am on Sep 23, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



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

phranque

12:13 pm on Sep 23, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



"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]