This is a discussion on Writing array to csv string within the PHP Language forums, part of the PHP Programming Forums category; Adam Levenstein wrote: > Hey all, > > I have need to take an array of strings from a form ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Adam Levenstein wrote:
> Hey all, > > I have need to take an array of strings from a form and write it to a > string (not a file) in csv format. Is there a quick n' easy way of > doing this? > > Thanks, > > Adam Have a look at the implode function, that should get you started. HTH, Derek |
|
|||
|
Adam Levenstein wrote:
> Hey all, > > I have need to take an array of strings from a form and write it to a > string (not a file) in csv format. Is there a quick n' easy way of > doing this? > > Thanks, > > Adam Something like the following should work... $csv=''; foreach($array as $v) $csv.=', \''.addslashes($v).'\''; $cvs=substr($csv,2); -- Justin Koivisto - spam@koivi.com PHP POSTERS: Please use comp.lang.php for PHP related questions, alt.php* groups are not recommended. |
|
|||
|
cleon42@yahoo.com (Adam Levenstein) wrote in message
news:<a32e730c.0407070759.7d74c5db@posting.google. com>... > > I have need to take an array of strings from a form and write it to a > string (not a file) in csv format. Is there a quick n' easy way of > doing this? Sure. The easy way is to use the implode() function, but this will create a problem if one or more of your array fields is a text containing a comma. So you need to enclose text fields. Assuming your form is submitted via POST, here's what you can do: $csvstring = ''; foreach ($_POST as $value) { if ($csvstring <> '') { $csvstring .= ','; } if (is_numeric($value)) { $csvstring .= $value; } else { $csvstring .= "'$value'"; } } Yet another alternative is to use implode, but enclose ALL fields: $csvstring = "'" . implode("','", $_POST) . "'"; Cheers, NC |