Forum Moderators: coopster

Message Too Old, No Replies

array to variable

cant think of anyway to convert this

         

Jaunty Edward

7:11 pm on May 31, 2008 (gmt 0)

10+ Year Member



Hi,

I think I am sure this is not at all tough to achieve, but since I have already wasted 3-4 hours please help me out with this.

I am submitting data from a form as $_POST being an array. Now I want two vars:

$fields = "name,username,password";
$values = "Juanty,jaunty,test";

I have done what not to get the values and keys in the variables but no luck. I can even print the values but not able to store them in a variable.

I am sure this should be super easy, please help.

Regards,
Jaunty

rob7591

8:06 pm on May 31, 2008 (gmt 0)

10+ Year Member



you can do:
$fields = "name,username,password";
$values = "Juanty,jaunty,test";
$a1 = explode(',',$fields);
$a2 = explode(',',$values);
$array = array_combine($a1, $a2);

/*
//Edit: Found the function for it, use the above
$array = array();
foreach ($a1 as $k=>$v) {
$array[$v] = $a2[$k];
}
*/
Not sure if there's a built in function, but that should achieve what you want.

Jaunty Edward

8:09 pm on May 31, 2008 (gmt 0)

10+ Year Member



Hi,

sorry if I did not make this clear before,

$fields = "name,username,password";
$values = "Juanty,jaunty,test";

is the desired output, actually I have a form with 3 input fields named: name, username, password and if I fill the values as Jaunty, jaunty, test I want to get these values in the variables $fields and $values.

Also the form is dynamically generated so there might be 3-5- or even 10 fields.

Regards,
Jaunty

rob7591

8:14 pm on May 31, 2008 (gmt 0)

10+ Year Member



Why don't you do:
$info['name'] = $_POST['name'];
$info['username'] = $_POST['username'];
$info['password'] = $_POST['password'];

Or better yet:

$info = $_POST;

if no other values are being passed.

Jaunty Edward

8:48 pm on May 31, 2008 (gmt 0)

10+ Year Member



HI,

thanks for you reply, but as I said the form is dynamic and can have any number of fields, they can be different as well.

secondly i can not capture them as $info = $_POST;

coz thats not giving comma separated values, it returns an array which is the same as $_POST.

Regards,
Jaunty

dreamcatcher

10:09 pm on May 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$f = array();
$v = array();

foreach ($_POST AS $key => $value) {
$f[] = $key;
$v[] = $value;
}

$fields = \"".implode(",",$f)."\";
$values = \"".implode(",",$v)."\";

Think that should work ok.

dc

rob7591

12:35 am on Jun 1, 2008 (gmt 0)

10+ Year Member



Oooh, I get it :}

Sorry, I didn't understand the question.

Ya what dream said should work. (except idk what those \"" are?)

Jaunty Edward

2:53 am on Jun 1, 2008 (gmt 0)

10+ Year Member



Hey,

dreamcatcher, the code worked the way I wanted, I just had to remove the the slashes:

$fields = implode(",",$f);
$values = implode(",",$v);

Thanks for your help..

Regards,
Jaunty

dreamcatcher

6:54 am on Jun 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, no problem. Not sure what I put the quotes there for. It was late last night. Glad you got it sorted.

dc