How to Pass an Array in URL Query String with PHP
In this short tutorial, you will find the way of passing an array in URL query string using PHP.
PHP includes the <kbd class="highlighted">http_build_query()</kbd> function for creating URL-encoded query strings. This function is available in PHP 5 and later. In this snippet, you will learn how to pass an array in a URL query string using PHP.
The syntax of <kbd class="highlighted">http_build_query()</kbd> is as follows:
php http_build_query() syntax
http_build_query($query, $numeric_prefix, $separator, $encoding)
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Learn object oriented PHP</div>
Passing a Simple Array within http_build_query()
In this section, we will use the <kbd class="highlighted">$vars</kbd> array to generate the URL query string. The <kbd class="highlighted">$vars</kbd> array contains a search keyword and a page number.
Let’s see an example where this array is passed to the <kbd class="highlighted">http_build_query()</kbd> function for generating a query string:
php create a query string with http_build_query()
<?php
$vars = ['page' => 26, 'search' => 'w3docs'];
$qs = http_build_query($vars);
$url = 'http://www.example.com/search.php?' . $qs;
echo $url;
?>The code, represented above will return the following url query string:
create url query string output
http://www.example.com/search.php?page=26&search=w3docsPassing an Indexed Array within http_build_query()
Now, let’s see an example where <kbd class="highlighted">$vars</kbd> is an indexed array containing employee data:
php indexed array, http_build_query()
<?php
$vars = ['employee', 'brown', 'developer', 'emp_id' => '332'];
$qs = http_build_query($vars);
$url = 'http://www.example.com/search.php?' . $qs;
echo $url;
?>The code above will return the following url query string:
php, create url query string output
http://www.example.com/search.php?0=employee&1=brown&2=developer&emp_id=332Passing a Multidimensional Array within http_build_query()
In this section, we will use <kbd class="highlighted">$vars</kbd> as a multidimensional array containing employee data. When passed to the <kbd class="highlighted">http_build_query()</kbd> function, it generates a complex URL query string.
Here is an example:
pass multidimensional array with http_build_query()
<?php
$vars = ['employee' => ['name' => 'Brown', 'age' => 41, 'dept' => 'IT', 'dob' => '9/22/1980'], 'role' => ['engineer', 'developer']];
$qs = http_build_query($vars);
$url = 'http://www.example.com/search.php?' . $qs;
echo $url;
?>This code will return the following url query string:
php url query string outcome
http://www.example.com/search.php?employee%5Bname%5D=Brown&employee%5Bage%5D=41&employee%5Bdept%5D=IT&employee%5Bdob%5D=9%2F22%2F1980&role%5B0%5D=engineer&role%5B1%5D=developerDescribing Query String
A query string is a component of a Uniform Resource Locator (URL). It consists of a series of key-value pairs. It is typically used to pass data to a URL or extract data from one.
You can use query strings for various purposes, such as search keywords, URL building, and page tracking. When the receiving page loads, PHP automatically parses this query string back into an array using the $_GET superglobal.