Parsing Query Strings into Arrays
Converting a query string into an array is a common task in web development. This allows you to easily access the individual parameters and their values.
Given a query string like the one below:
pg_id=2&parent_id=2&document&video
You can use the parse_str function to transform it into an array:
$queryString = "pg_id=2&parent_id=2&document&video"; parse_str($queryString, $queryArray);
By setting the second parameter of parse_str to an empty array, you instruct it to store the parsed data in the specified variable rather than overwriting existing variables.
The resulting array, $queryArray, will have the following structure:
array( 'pg_id' => 2, 'parent_id' => 2, 'document' => '', 'video' => '' )
This array can now be used to conveniently access and manipulate the query string parameters.
The above is the detailed content of How to Parse Query Strings into Arrays in PHP?. For more information, please follow other related articles on the PHP Chinese website!