Giganews Newsgroups

Writing array to csv string

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-07-2004
Derek Battams
 
Posts: n/a
Default Re: Writing array to csv string

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
Reply With Quote
  #2 (permalink)  
Old 07-07-2004
Adam Levenstein
 
Posts: n/a
Default Writing array to csv string

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
Reply With Quote
  #3 (permalink)  
Old 07-07-2004
Justin Koivisto
 
Posts: n/a
Default Re: Writing array to csv string

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.
Reply With Quote
  #4 (permalink)  
Old 07-09-2004
Nikolai Chuvakhin
 
Posts: n/a
Default Re: Writing array to csv string

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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 05:46 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0