Open In App

PHP Program to Convert Array to Query String

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

This article will show you how to convert an array to a query string in PHP. The Query String is the part of the URL that starts after the question mark(?).

Example:

Input: $arr = (
'company' => 'GeeksforGeeks',
'Address' => 'Noida',
'Phone' => '9876543210'
);
Output: company=GeeksforGeeks&Address=Noida&Phone=9876543210

There are two methods to convert an array to query strings, these are:

Using http_build_query() Method

The http_build_query() function is used to generate a URL-encoded query string from the associative (or indexed) array.

Syntax:

string http_build_query( 
$query_data,
$numeric_prefix,
$arg_separator,
$enc_type = PHP_QUERY_RFC1738
)

Example: This example uses http_build_query() Method to convert associative array into query sting.

PHP
<?php 

$arr = array( 
    'company' => 'GeeksforGeeks', 
    'Address' => 'Noida', 
    'Phone' => '9876543210'
); 

$queryStr = http_build_query($arr); 

echo $queryStr; 

?>

Output
company=GeeksforGeeks&Address=Noida&Phone=9876543210

Using urlencode() and rtrim() Methods

First, we will create an empty string and then use foreach loop to merge key and encoded values with equals and & symbol and at least trim the & symbol from right side.

  • The urlencode() function is used to encode the URL. This function returns a string which consist all non-alphanumeric characters except -_. and replace by the percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.
  • The rtrim() function is used to remove the white spaces or other characters (if specified) from the right side of a string.

Syntax:

string urlencode( $input )
rtrim( $string, $charlist )

Example: This example uses urlencode() and rtrim() methods to convert associative array into query sting.

PHP
<?php 

// Crreate an Array 
$arr = array( 
    'company' => 'GeeksforGeeks', 
    'Address' => 'Noida', 
    'Phone' => '9876543210'
); 

// Create an empty string 
$queryStr = ''; 

// Loop to iterate array elements 
foreach ($arr as $key => $value) { 
    $queryStr .= $key . '=' . urlencode($value) . '&'; 
} 

// Remove the & symbol of right side 
$queryStr = rtrim($queryStr, '&'); 

// Print the Query String 
echo $queryStr; 

?>

Output
company=GeeksforGeeks&Address=Noida&Phone=9876543210

Using array_map() and implode() Methods

This approach involves mapping the array elements to key-value pairs and then using implode to join them into a query string.

Example: The example usage demonstrates creating a query string using array_map() and implode().

PHP
<?php 
$arr = array( 
    'company' => 'GeeksforGeeks', 
    'Address' => 'Noida', 
    'Phone' => '9876543210'
); 

$queryStr = implode('&', array_map(
    function($key, $value) {
        return $key . '=' . urlencode($value);
    },
    array_keys($arr),
    $arr
));

echo $queryStr; 
?>

Output:

company=GeeksforGeeks&Address=Noida&Phone=9876543210

Using array_reduce() Method

The array_reduce() function iteratively reduces the array to a single string using a callback function.

Example: This example uses the array_reduce() method to convert an associative array into a query string.

PHP
<?php 
$arr = array( 
    'company' => 'GeeksforGeeks', 
    'Address' => 'Noida', 
    'Phone' => '9876543210'
); 

$queryStr = array_reduce(
    array_keys($arr),
    function($carry, $key) use ($arr) {
        return $carry . $key . '=' . urlencode($arr[$key]) . '&';
    },
    ''
);

// Remove the trailing & symbol
$queryStr = rtrim($queryStr, '&');

echo $queryStr; 
?>

Output
company=GeeksforGeeks&Address=Noida&Phone=9876543210

Using array_walk() Method

The array_walk() function applies a user-defined function to every element of the array. In this approach, we'll build the query string by concatenating the key-value pairs within the callback function.

Syntax:

bool array_walk(array &$array, callable $callback, mixed $userdata = null)

Example: This example uses the array_walk() method to convert an associative array into a query string.

PHP
<?php 

$arr = array( 
    'company' => 'GeeksforGeeks', 
    'Address' => 'Noida', 
    'Phone' => '9876543210'
); 

$queryStr = '';

array_walk($arr, function($value, $key) use (&$queryStr) {
    $queryStr .= $key . '=' . urlencode($value) . '&';
});

// Remove the trailing & symbol
$queryStr = rtrim($queryStr, '&');

echo $queryStr;

?>

Output
company=GeeksforGeeks&Address=Noida&Phone=9876543210

Next Article

Similar Reads