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',
)
asked Jan 29, 2011 at 18:09
Alex
66.6k187 gold badges463 silver badges655 bronze badges
-
are you getting request maybe? or not? :)FeRtoll– FeRtoll2011年01月29日 18:16:04 +00:00Commented Jan 29, 2011 at 18:16
2 Answers 2
Use parse_str
parse_str('single=Single&multiple=Multiple2&check=check1&radio=radio2', $data);
And in $data you will have your variables.
Sign up to request clarification or add additional context in comments.
1 Comment
nv1t
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 :)
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.
answered Jan 29, 2011 at 18:16
Elzo Valugi
28.1k17 gold badges97 silver badges115 bronze badges
4 Comments
Alex
it comes from a hidden form field generated with jquery, like this: api.jquery.com/serialize
Elzo Valugi
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.
Alex
yes, I do, but only the hidden input (it's all I need). all the other inputs are there just to build this string :)
Elzo Valugi
it does not matter, those jquery functions send them as POST or GET parameters depending on your request.
Explore related questions
See similar questions with these tags.
lang-php