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
turning an array into a string then poping it into a db
Moderator: General Moderators
Re: turning an array into a string then poping it into a db
Code: Select all
$string = implode($_REQUEST['choice_name']);Re: turning an array into a string then poping it into a db
thanks that worked great is there any way of adding something like <br. or [space] or - into it?
Re: turning an array into a string then poping it into a db
Code: Select all
$myString = "";
$delimiter = " ";
foreach($_REQUEST['choice_name'] as $value){
$myString .= $value.$delimiter;
}
print $myString;So you would change
Code: Select all
$delimiter = " ";Code: Select all
$delimiter = ":";Re: turning an array into a string then poping it into a db
how do i add the 2 together tho?
Ive done as you suggested it just keeps saying Array at teh begining its SO ANNOYING
Ive done as you suggested it just keeps saying Array at teh begining its SO ANNOYING
Re: turning an array into a string then poping it into a db
What code are you using?
- 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
Hello gotornot,
Using implode will not get you the format you specified in the op.
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.
Using implode will not get you the format you specified in the op.
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).Colour=Blue Size=8 Neck Line=V
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.