Serialized string to array in PHP

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

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

How can I translate it into a real array?

I was thinking of replacing & with ; and approval. Any best deals?

+4
source share
3 answers

You are looking for parse_str() .

Parses str, as if it is a query string passed through a URL, and sets the variables in the current scope.

For instance:

 $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 the results if you do not want to pollute the scope:

 parse_str($str, $output); echo $output['first']; // value echo $output['arr'][0]; // foo bar echo $output['arr'][1]; // baz 
+15
source

Use parse_str :)

0
source
 // I am assuming you are getting this value from post or get method; $string = implode(";", $_REQUEST['items']); echo $string; 
0
source

All Articles