[thelist] PHP explode, and name value pairs

Hans Zaunere zaunere at yahoo.com
Thu Apr 18 16:03:14 CDT 2002


I hope you're not trying to put multiple values in a single SQL field.
*grin*

I think what you've said would do it - as for the best way, I would try
to avoid this string/storing/mangling.  But if I had to real quick:

$string = "name1:val1:name2:val2:name3:val3";
$arr = explode(':', $string);
for( $i = 0; $i < count($arr); ++$i )
  $newarr[$arr[$i]] = $arr[++$i];

You could always do it without explode() (for() loop across the string)
which would probably be faster for REALLY large data sets.

Of course assuming you'll never have an ':' in any of your values.

Hans Z.
New York PHP
http://nyphp.org




--- Donald Noble <donaldrnoble at yahoo.com> wrote:
>
> Hi evolters, hope you can help me with this...
>
> Using PHP, what is the 'best' way to handle a series of name value
> pairs
> that are to be stored as a single string but need to be manipulated
> as
> separate items. To make things more complicated there will be a
> variable
> number of name value pairs.
>
> I have used the explode function to create an array of items from a
> list, but I am unsure how best to extend this to name value pairs.
>
> The 2 ideas I had had were:
>
> Store the data in the form  name1:value1/name2:value2/name3:value3
> then
> split this up into an array so that array['name1']==value1 etc.
>
> Alternatively store the data as
> name1:name2:name3/value1:value2:value3
> and then split the data into 2, then into 2 arrays so that
> names[0]==name1 and values[0]==value1
>
> Which of these would be easier/faster/better - or is there another
> way
> to do this altogether
>
> Cheers for your help, Donald
>
> --
>      http://www.AccessibleComputers.co.uk
>  Hardware, Software & Custom built PC Systems
>
>
>
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
>
> --
> For unsubscribe and other options, including
> the Tip Harvester and archive of thelist go to:
> http://lists.evolt.org Workers of the Web, evolt !


__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/



More information about the thelist mailing list