Home >Backend Development >PHP Tutorial >How to Parse Query Strings into Arrays in PHP?

How to Parse Query Strings into Arrays in PHP?

DDD
DDDOriginal
2024-12-18 15:10:15651browse

How to Parse Query Strings into Arrays in PHP?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn