5

I receive a string from jQuery UI Sortable plugin, it gives me a string like this one

items[]=1&items[]=2&items[]=3

How can I trun it into a real array?

I was thinking replacing & with ; and asserting it. Any better suggestions?

1
  • You should receive this as $_POST or $_GET, how are you even sending the output of sortable to your php in the first place? Commented May 4, 2011 at 13:34

3 Answers 3

21

You are looking for parse_str().

Parses str as if it were the query string passed via a URL and sets variables in the current scope.

For example:

$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first;  // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz

You can also specify an array to store results in if you do not want to pollute your scope:

parse_str($str, $output);
echo $output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
Sign up to request clarification or add additional context in comments.

Comments

1

Use parse_str :)

1 Comment

@Phoenix I think it's pretty sure to link to the php doc :)
-1
// I am assuming you are getting this value from post or get method;

$string = implode(";", $_REQUEST['items']);
echo $string;

Comments

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.