How can I turn a string below into an array?

pg_id=2&parent_id=2&document&video 

This is the array I am looking for,

array(
'pg_id' => 2,
'parent_id' => 2,
'document' => ,
'video' =>
)
share|improve this question

feedback

4 Answers

up vote 8 down vote accepted

You want the parse_str function, and you need to set the second parameter to have the data put in an array instead of into individual variables.

 $get_string = "pg_id=2&parent_id=2&document&video";

 parse_str($get_string, $get_array);

 print_r($get_array);
share|improve this answer
feedback

parse_str

share|improve this answer
1  
+1: Darn, you beat me :-P – Rocket Hazmat Mar 22 '11 at 20:58
feedback

Using parse_str().

$str = 'pg_id=2&parent_id=2&document&video';
parse_str($str, $arr);
print_r($arr);
share|improve this answer
feedback

There are several possible methods, but for you, there is already a builtin function

$array = array();
parse_str($string, $array);
var_dump($array);
share|improve this answer
feedback

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.