How to pass an array in a URL query string using PHP
In this article, you will learn how to pass an array from one page to another in a URL query string using PHP.
You will learn not only how to pass a single array, but also learn to pass indexed and multidimensional arrays to an URL query string using PHP.
The term 'Query String' is a part of a Uniform Resource Locator(URL). This is in the form of a series of key-value pairs and is called as url parameter. This is basically used to build a typical url or to get data from url. For example-
https://www.example.com/emp?name=Smith&dept=it&role=developer&exp=5
In the above url, 'name=Smith&dept=it&role=developer&exp=5' is the query string, having multiple parameters. The parameters are separated by '&' and within each pair, the field name and value are separated by an equal sign(=).
The query string is used for many purposes, like the form get method, which takes input field values in the url query string, search keywords, particular web page tracking, url building, and so on.
In HTML, you have observed how the form input name with value is passed in a URL query string if the form method is post. But, this generates only a static query string. To build query strings dynamically, methods are provided by almost all popular programming languages. In this article, you will learn how to pass an array variables in a URL query string using PHP.
PHP pass an array in a URL query string using http_build_query() function
PHP provides the http_build_query() method to generate URL-encoded query string. This is available with PHP 5 and later versions. The http_build_query() function converts an array to its equivalent URL-encoded query string.
Syntax of http_build_query()
http_build_query($query, $numeric_prefix, $separator, $encoded_type)
$query- It is an associative array or indexed array containing parameters. This can be a one-dimensional array or a multidimensional array.
$numeric_prefix- A numeric prefix is provided if numeric indices are used in the base array.
$separator- This overrides the default parameter separator.
$encoded_type- This is the query string encoding type, like- PHP_QUERY_RFC1738, PHP_QUERY_RFC3986.
Pass simple array in http_build_query()
In the below example, we have used the $vars array to create the URL query string. The $vars array contains data on search keywords and page numbers. In the next line, we pass this array to the http_build_query() method to generate a query string.
<?php
$vars = array('page' => 23, 'search' => 'etutorialspoint');
$qs = http_build_query($vars);
$url = 'http://www.example.com/search.php?' . $qs;
echo $url;
?>
The above code returns this url query string -
http://www.example.com/search.php?page=23&search=etutorialspoint
Pass indexed array in http_build_query()
Here, $vars is an indexed array containing employee data.
<?php
$vars = array('employee','smith','technical','emp_id' => '332');
$qs = http_build_query($vars);
$url = 'http://www.example.com/search.php?' . $qs;
echo $url;
?>
The above code returns this url query string-
http://www.example.com/search.php?0=employee&1=smith&2=technical&emp_id=332
Pass multidimentional array in http_build_query()
Here, the '$vars' is a multidimensional array containing employee data. This is returned by the http_build_query() method as a complex url query string.
<?php
$vars = array('employee'=>array('name'=>'Smith',
'age'=>39,
'dept'=>'IT',
'dob'=>'9/22/1980'),
'role'=>array('developer', 'designer'));
$qs = http_build_query($vars);
$url = 'http://www.example.com/search.php?' . $qs;
echo $url;
?>
The above script returns this url query string-
http://www.example.com/search.php?employee%5Bname%5D=Smith&employee%5Bage%5D=39&employee%5Bdept%5D=IT&employee%5Bdob%5D=9%2F22%2F1980&role%5B0%5D=developer&role%5B1%5D=designer
PHP pass an array in a URL query string using serialize() and urlencode() functions
We can also use the urlencode() function with the serialize() function to create an URL-encoded string. In the given example, first we have converted the array into its byte stream representation, which is a string using the serialize() function. And then, we have created an URL-encoded string of that byte stream.
<?php
$vars = array('page' => 40, 'search' => 'etutorialspoint');
echo "http://www.example.com/search.php?" . urlencode(serialize($vars));
?>
Output of the above code:
http://www.example.com/search.php?a%3A2%3A%7Bs%3A4%3A%22page%22%3Bi%3A40%3Bs%3A6%3A%22search%22%3Bs%3A15%3A%22etutorialspoint%22%3B%7D
Related Articles
PHP sanitize input for MySQLPHP random quote generator
PHP String Contains
PHP calculate percentage of total
PHP Fix: invalid argument supplied for foreach
Locking files with flock()
How to Pass an Array as URL Parameter in PHP
How to generate pdf in PHP using MySQL and MPDF Library
How to create search filter in PHP
How to convert associative array to XML in PHP
Get Visitor Information by IP Address in PHP
Getting Document of Remote Address
Get Visitor's location and TimeZone
Get current visitor's location using HTML5 Geolocation API and PHP
JavaScript display PDF in the browser using Ajax call
jQuery loop over JSON result after AJAX Success
Simple star rating system using PHP, jQuery and Ajax
jQuery File upload progress bar with file size validation
Print section of page using javascript
Submit a form data without page refresh using PHP, Ajax and Javascript