Open In App

How to Convert Query String to an Array in PHP?

Last Updated : 20 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see how to convert a query string to an array in PHP. The Query String is the part of the URL that starts after the question mark(?).

Examples:

Input: str = "company=GeeksforGeeks&address=Noida&mobile=9876543210"
Output: Array (
[company] => GeeksforGeeks
[address] => noida
[mobile] => 9876543210
)
Input: str = "name=xyz&language=english&age=23&education=btech"
Output: Array(
[name] => xyz
[language] => english
[age] => 23
[education] => btech
)

Below are the methods to convert a query string to an array:

Using parse_str() Function

The parse_str() function parses a query string into variables. The string passed to this function for parsing is in the format of a query string passed via a URL.

Syntax:

void parse_str($string, $array)

Example 1: This example uses parse_str() function to convert the query string into the array variable.

<?php 

// Query String 
$query = "name=xyz&address=noida&mobile=9876543210"; 

// Parses a query string into variables 
parse_str($query, $res); 

// Print the result 
print_r($res); 

?>

Output
Array
(
    [name] => xyz
    [address] => noida
    [mobile] => 9876543210
)

Example 2: This example uses parse_str() function to convert the query string into the array and store into $arr variable.

Using $_GET Method

The GET method sent the data as URL parameters that are usually strings of name and value pairs separated by ampersands (&). We can display the get data using print_r() function.

Example: This example uses $_GET method to convert the query string to the array.

<?php 

if (!empty($_GET)) { 
    print_r($_GET); 
} 
else { 
    echo "No GET data passed!"; 
} 

?>

Output:

querystring

Using explode() and Custom Parsing

For a manual approach, you can use explode() to split the query string and then parse it.

Example: Manually parsing the query string using explode().

Output:

Array
(
[name] => xyz
[address] => noida
[mobile] => 9876543210
)

Using http_build_query and parse_url

This PHP method converts a query string to an array using parse_url to extract query parameters and parse_str to convert them into an associative array.

Example: This example shows the implementation of the above-mentioned approach.


Output
Array
(
    [name] => Geek
    [age] => 25
    [city] => Noida
)

Using json_decode() with json_encode()

Another interesting approach to convert a query string to an array is by converting the query string to JSON format first and then decoding it into an associative array using json_decode().

Example: This approach adds another layer of versatility to the task of converting a query string into an associative array in PHP, utilizing JSON encoding and decoding for accurate and efficient conversion.


Output
Array
(
    [company] => GeeksforGeeks
    [Address] => Noida
    [Phone] => 9876543210
)

Using preg_match_all and Regular Expressions

This approach uses regular expressions with preg_match_all to match all key-value pairs in the query string and then parse them into an associative array.

Example: This example demonstrates how to convert a query string to an array using preg_match_all:


Output
Array
(
    [name] => xyz
    [language] => english
    [age] => 23
    [education] => btech
)

Next Article

Similar Reads