Reply to Re: How to convert array to string, and vice versa

Your name:

Reply:


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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация