Home >Backend Development >PHP Tutorial >How to Convert a Query String to an Array in PHP?

How to Convert a Query String to an Array in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-12-26 13:46:10556browse

How to Convert a Query String to an Array in PHP?

Convert Query String to an Array

Problem Statement:

Convert the query string pg_id=2&parent_id=2&document&video into an array. The expected output is:

array(
    'pg_id' => 2,
    'parent_id' => 2,
    'document' => '',
    'video' => ''
)

Solution Using parse_str:

The parse_str function allows you to parse a query string and assign the values to variables. To store the data in an array instead of variables, set the second parameter to TRUE.

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

parse_str($queryString, $queryArray, TRUE);

print_r($queryArray);

The above is the detailed content of How to Convert a Query String to an Array 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