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
Release: 2024-12-26 13:46:10
Original
550 people have browsed it

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

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

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!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template