Re: How to convert array to string, and vice versa
- From: Tim Streater <tim.streater@xxxxxxxxxxxx>
- Date: Tue, 23 May 2006 18:46:59 +0100
In article <lfHcg.74213$ZF.33611@xxxxxxxxxxxxxxxxxxxxx>,
David Haynes <david.haynes2@xxxxxxxxxxxx> wrote:
Tim Streater wrote:
I have some items, numbered from 0 upwards. Some of them may have a
string attached. All these items need to be represented in a single
already existing database record. So, I thought of taking an array, as
it might be looking thus (the values are all strings):
Key Value
--- -----
0 firstone
2 somestring
5 anotherstr
and so on, and converting it to a single string:
"'0', 'firstone', '2', 'somestring', '5', 'anotherstr'"
then I have a string I can write to the database record.
So I want to go from:
$myarr = array (0 => 'firstone', 2 => 'somestring', 5 => 'anotherstr');
to:
$mystring = "'0', 'firstone', '2', 'somestring', '5', 'anotherstr'";
Is there a quick way to do this mapping in both directions? (I don't
mind if the string read/written to the database is not as above, as it
won't be used for any other purpose).
I can't see any array or string function that looks designed for this or
a similar purpose.
Thanks for any pointers.
-- tim
Something like this could be used to pack the string:
$foreach( $myarr as $key => $value ) {
$tmp[] = "'$key'";
$tmp[] = "'$value'";
}
$mystring = '"'.implode(', ', $tmp).'"';
Unpacking would be something like:
$tmp = substr($mystring, 1, strlen($mystring)-1);
$myarr = explode(', ', $tmp);
You may have to fudge it a bit to get the quotes all correct.
David,
No time to look at that carefully tonight but I'll check it tomorrow.
Thanks,
-- tim
.
- References:
- How to convert array to string, and vice versa
- From: Tim Streater
- Re: How to convert array to string, and vice versa
- From: David Haynes
- How to convert array to string, and vice versa
- Prev by Date: Re: How to convert array to string, and vice versa
- Next by Date: problems with ctype_print
- Previous by thread: Re: How to convert array to string, and vice versa
- Next by thread: Re: How to convert array to string, and vice versa
- Index(es):
Relevant Pages
|