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

How to Parse Query Strings into Arrays in PHP?

DDD
Release: 2024-12-18 15:10:15
Original
647 people have browsed it

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
Copy after login

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);
Copy after login

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' => ''
)
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template