turning an array into a string then poping it into a db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gotornot
Forum Commoner
Posts: 54
Joined: Fri Jul 31, 2009 2:30 am

turning an array into a string then poping it into a db

Post by gotornot »

Hi im just wondering how you get a requested array into a single string then pop it into the db so far i got
print_r($_REQUEST['choice_name']);

whch echos

Array ( [0] => Colour=Blue [1] => Size=8 [2] => Neck Line=V )

I need it to look ready to enter into a db in a single field like this

Colour=Blue Size=8 Neck Line=V

Any ideas
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: turning an array into a string then poping it into a db

Post by Weiry »

Code: Select all

$string =  implode($_REQUEST['choice_name']);
This is of course that you want everything just put in a string one item after another...
gotornot
Forum Commoner
Posts: 54
Joined: Fri Jul 31, 2009 2:30 am

Re: turning an array into a string then poping it into a db

Post by gotornot »

thanks that worked great is there any way of adding something like <br. or [space] or - into it?
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: turning an array into a string then poping it into a db

Post by Weiry »

Code: Select all

$myString = "";
$delimiter = " ";
foreach($_REQUEST['choice_name'] as $value){
   $myString .= $value.$delimiter;
}
print $myString;
But i wouldn't use a space for your delimiter (separating your array values), you would be better off using a comma or colon etc.
So you would change

Code: Select all

$delimiter = " ";
to be

Code: Select all

$delimiter = ":";
as an example
gotornot
Forum Commoner
Posts: 54
Joined: Fri Jul 31, 2009 2:30 am

Re: turning an array into a string then poping it into a db

Post by gotornot »

how do i add the 2 together tho?
Ive done as you suggested it just keeps saying Array at teh begining its SO ANNOYING
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: turning an array into a string then poping it into a db

Post by jackpf »

What code are you using?
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: turning an array into a string then poping it into a db

Post by PHPHorizons »

Hello gotornot,

Using implode will not get you the format you specified in the op.

Colour=Blue Size=8 Neck Line=V
McInfo has pointed out that you can use serialize() to convert an array to a string, and unserialize() to convert back to an array. This is the prefered way to convert arrays to strings when you need to preserve the keys (implode blows away your keys).

If you are using php >= 5.2, then json_encode() and json_decode() are better than serialize/unserialize in many cases. (just make sure to use "true" for the second parameter of json_decode in order to get an array instead of an object: json_decode($string, true))

Hope that helps.
Post Reply