I have a string that looks like:

single=Single&multiple=Multiple2&check=check1&radio=radio2

how could I create a array like this:

array(
  'single' => 'Single',
  'multiple' => 'Multiple2',
  'check' => 'check1',
  'radio' => 'radio2',
)
share|improve this question
are you getting request maybe? or not? :) – FeRtoll Jan 29 '11 at 18:16

2 Answers

up vote 19 down vote accepted

Use parse_str

parse_str('single=Single&multiple=Multiple2&check=check1&radio=radio2', $data);

And in $data you will have your variables.

share|improve this answer
nice answer! short and good! ;) +1 – FeRtoll Jan 29 '11 at 18:15
thank you singles – Alex Jan 29 '11 at 18:19

If this comes from an URL you can have this already as an array in the $_GET or $_POST variables. Otherwise use explode() to convert string to an array.

share|improve this answer
it comes from a hidden form field generated with jquery, like this: api.jquery.com/serialize – Alex Jan 29 '11 at 18:18
if you are using ajax(), get() or post() to send it to php you still should have it in the global vars. As an extra suggestion, filter_var the vars that come from js, before using them. – Elzo Valugi Jan 29 '11 at 18:57
yes, I do, but only the hidden input (it's all I need). all the other inputs are there just to build this string :) – Alex Jan 29 '11 at 18:59
it does not matter, those jquery functions send them as POST or GET parameters depending on your request. – Elzo Valugi Jan 29 '11 at 19:03

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.