|
Posted by tim on 12/18/54 11:48
David Haynes wrote:
> Tim Streater wrote:
> > So I want to go from:
> >
> > $myarr = array (0 => 'firstone', 2 => 'somestring', 5 => 'anotherstr');
> >
> > to:
> >
> > $mystring = "'0', 'firstone', '2', 'somestring', '5', 'anotherstr'";
>
> 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);
array keys from the original $myarr are now array values in the new
$myarr
> You may have to fudge it a bit to get the quotes all correct.
The quotes are a problem
> -david-
What Oli said is right but if you want to do it this way then David's
suggestion of using a foreach/implode/explode would work.
You could make the packing and unpacking, especially the unpacking,
easier and cleaner if you do not quote the keys/values and if you use
two seperators. For example & and = from url query strings.
Tim
Navigation:
[Reply to this message]
|