6

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',
)
1
  • are you getting request maybe? or not? :) Commented Jan 29, 2011 at 18:16

2 Answers 2

22

Use parse_str

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

And in $data you will have your variables.

1
  • Keep in Mind: without using $data you will create a security issue, coz you are loading data into your current scope, so it is crucial to use it :) Commented Jul 1, 2016 at 7:06
5

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.

4
  • it comes from a hidden form field generated with jquery, like this: api.jquery.com/serialize Commented Jan 29, 2011 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. Commented Jan 29, 2011 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 :) Commented Jan 29, 2011 at 18:59
  • it does not matter, those jquery functions send them as POST or GET parameters depending on your request. Commented Jan 29, 2011 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.