473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Jul 17 '05 #1
3 29104
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
Jul 17 '05 #2
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 - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
Jul 17 '05 #3
cl*****@yahoo.com (Adam Levenstein) wrote in message
news:<a3**************************@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
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: rob | last post by:
hey every1, I've got alot of data to write out to file and it's all just 1's and 0's. It's all stored in 2 dimensional arrays of width 32 and varying height. At the moment it's all just...
3
by: Joshua Russell | last post by:
Hi, I've got a program (see source below) that makes a file and fills it with random binary values (from 0 to 255). The source below works, however the program creates files at a rate of about...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
3
by: Bry | last post by:
Having mastered reading a multi_sz from the registry into an array using: // Max of 999 is fine in this instance string myArray = new string; myArray = (string) (myRegKey.GetValue("RegValue"));...
2
by: simonc | last post by:
Is there an easy way of writing a number in 32 bit integer format into four bytes of a file? I am experimenting with FilePut but I can only make it work by defining a four byte array and doing some...
2
by: cindy | last post by:
i build a string string eProduct = "#blah#ablah#bbbb"; StringBuilder sb = new StringBuilder(eProduct); eProduct = sb.Replace("#","").ToString(); string aProducts= eProduct.Split(';'); then I...
9
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
3
by: mintominto82 | last post by:
Hello, I am a newbie to C++. I read C++ Primer from cover to cover at least 3 times, but that did not help. I am trying to write a program that loads txt file and write the data into array. the data...
16
by: billsahiker | last post by:
I am researching for an upcoming c# .net 2.0 project that may require reading and writing xml files. I don't want to use xmltextreader/ xmltextwriter as I prefer to have lower level file access...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.