"Convert PHP Query String to Array: A Comprehensive Guide"

Sandra Jun 29, 2026

Converting PHP Query Strings to Arrays: A Comprehensive Guide

How To Convert Array To String In PHP
How To Convert Array To String In PHP

In the realm of web development, PHP is a powerful language that enables you to create dynamic and interactive web applications. One common task in PHP is handling query strings, which are used to pass data from one page to another. This article will guide you through the process of converting PHP query strings to arrays, a crucial step in extracting and managing data from query strings.

How to Convert a String to Byte Array in C#: Encoding and Decoding Simplified
How to Convert a String to Byte Array in C#: Encoding and Decoding Simplified

Understanding PHP Query Strings

Before we dive into converting query strings to arrays, let's first understand what query strings are. A query string is a component of a URL that appears after the question mark (?). It consists of key-value pairs, separated by ampersands (&), and is used to send data to the server. For example, in the URL https://example.com/?name=John&age=30, 'name' and 'age' are keys, while 'John' and '30' are their respective values.

estrutura de dados
estrutura de dados

Accessing Query Strings in PHP

PHP provides the global $_GET superglobal array to access query strings. This array is populated automatically with the data from the query string when the page is accessed. To access the data, you simply refer to the array keys. For instance, in the URL above, you can access the values like this:

💜 Save this f-Strings Cheat Sheet for later!
💜 Save this f-Strings Cheat Sheet for later!

echo $_GET['name']; // Outputs: John
echo $_GET['age']; // Outputs: 30

However, this method only works if you know the keys in advance. What if you want to access all the data in the query string, regardless of the keys? This is where converting the query string to an array comes in handy.

Converting PHP Query Strings to Arrays

PHP provides a built-in function called parse_str that can convert a query string into an array. The function takes two arguments: the query string and an array to populate with the data. Here's a simple example:

6 Ways To Read Files In PHP
6 Ways To Read Files In PHP

$query_string = 'name=John&age=30&city=New%20York';
parse_str($query_string, $output_array);

print_r($output_array);
// Outputs:
// Array
// (
//     [name] => John
//     [age] => 30
//     [city] => New York
// )

The parse_str function decodes the query string and populates the $output_array with the data. The keys are the same as in the query string, and the values are the decoded strings.

Handling Multiple Values

What happens if a key has multiple values? For example, consider the query string name=John&name=Jane&age=30. The parse_str function converts this into an array where the key 'name' is an array itself, containing both values:

JavaScript Arrays: Master Data Storage & Manipulation 📊
JavaScript Arrays: Master Data Storage & Manipulation 📊

parse_str('name=John&name=Jane&age=30', $output_array);
print_r($output_array);
// Outputs:
// Array
// (
//     [name] => Array
//         (
//             [0] => John
//             [1] => Jane
//         )
//     [age] => 30
// )

This behavior can be useful when you want to handle multiple values for a single key.

Removing Special Characters

an array is shown with the words arrays and lists in front of each other
an array is shown with the words arrays and lists in front of each other
an array of different types of characters on a black background
an array of different types of characters on a black background
Array implementation
Array implementation
an image of a computer screen with the words, using join and combine strings in python
an image of a computer screen with the words, using join and combine strings in python
Difference between Array and String
Difference between Array and String
Day 2 of DSA - Strings
Day 2 of DSA - Strings
What are Arrays in Java?
What are Arrays in Java?
Append To String Python Examples
Append To String Python Examples
ARRAY
ARRAY
Sorting Arrays in JavaScript
Sorting Arrays in JavaScript
a white board with numbers on it and an arrow pointing up to the left side
a white board with numbers on it and an arrow pointing up to the left side
Sort a NumPy Array
Sort a NumPy Array
Day 1 of dsa - Arrays
Day 1 of dsa - Arrays
Array Data Structure - GeeksforGeeks
Array Data Structure - GeeksforGeeks
an image of a computer screen with the text convert list to a string in python
an image of a computer screen with the text convert list to a string in python
Creating NumPy Arrays
Creating NumPy Arrays
the array data structure is displayed on a blackboard with numbers and symbols in it
the array data structure is displayed on a blackboard with numbers and symbols in it
a blackboard with the words string operators in pyron and an image of a woman's head
a blackboard with the words string operators in pyron and an image of a woman's head
Learn Arrays in C++ in 10 minutes.
Learn Arrays in C++ in 10 minutes.
the settings for wordpress are shown here
the settings for wordpress are shown here

Query strings can contain special characters that need to be decoded before they can be used. The parse_str function automatically decodes the values, converting percent-encoded characters (like %20 for a space) into their corresponding characters. For example, the query string city=New%20York is decoded to city=New York.

Conclusion

Converting PHP query strings to arrays is a powerful technique that allows you to manage and manipulate the data in query strings more effectively. Whether you're extracting data from a URL, handling form submissions, or working with APIs, understanding how to convert query strings to arrays is a crucial skill in PHP development.

In this article, we've covered the basics of query strings, how to access them in PHP, and how to convert them to arrays using the parse_str function. With this knowledge, you're ready to take on even the most complex query string challenges in your PHP projects.