etutorialspoint
  • Home
  • PHP
  • MySQL
  • MongoDB
  • HTML
  • Javascript
  • Node.js
  • Express.js
  • Python
  • Jquery
  • R
  • Kotlin
  • DS
  • Blogs
  • Theory of Computation

How to pass array in URL query string using PHP

In this article, you will learn how to pass array from one page to another in URL query string using PHP. You will learn not only the single array, but also learn to pass indexed and multidimensional arrays to URL query string using PHP.

The term 'Query String' is the part of a Uniform Resource Locator(URL). This is in the form of a series of key value pairs and called as url parameters. 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 takes input field values in url query string, search keywords, particular web page tracking, url building, and so on.

In HTML, you have observed how form input name with value passes in url query string if form method is post. But, this generates only static query string. To build query string dynamically, methods are provided by almost all popular programming languages. In this article, you will learn how to pass an array variables in URL query string using PHP.

How to pass array in URL query string using PHP

PHP provides http_build_query() method to generate URL encoded query string. This is available with PHP 5 and later versions.



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 one dimensional array or 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 $vars array to create the URL query string. The $vars array contains data of search keyword and page number. In the next line, we have passed this array to http_build_query() method to generate 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 contains employee data. When this will pass through http_build_query() method, it returns 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




Related Articles

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




Most Popular Development Resources
Characteristics of a Good Computer Program
-----------------
Retrieve Data From Database Without Page refresh Using AJAX, PHP and Javascript
-----------------
PHP MySQL PDO Database Connection and CRUD Operations
-----------------
How to get data from XML file in PHP
-----------------
Hypertext Transfer Protocol Overview
-----------------
PHP Create Word Document from HTML
-----------------
PHP code to send email using SMTP
-----------------
How to encrypt password in PHP
-----------------
Create Dynamic Pie Chart using Google API, PHP and MySQL
-----------------
Splitting MySQL Results Into Two Columns Using PHP
-----------------
How to get current directory, filename and code line number in PHP
-----------------
Dynamically Add/Delete HTML Table Rows Using Javascript
-----------------
Get current visitor\'s location using HTML5 Geolocation API and PHP
-----------------
How to Sort Table Data in PHP and MySQL
-----------------
PHP MYSQL Advanced Search Feature
-----------------
Simple star rating system using PHP, jQuery and Ajax
-----------------
Simple pagination in PHP with MySQL
-----------------
Fibonacci Series Program in PHP
-----------------
jQuery loop over JSON result after AJAX Success
-----------------
How to add multiple custom markers on google map
-----------------
PHP user registration and login/ logout with secure password encryption
-----------------
Submit a form data using PHP, AJAX and Javascript
-----------------
Recover forgot password using PHP7 and MySQLi
-----------------
Php file based authentication
-----------------
PHP Secure User Registration with Login/logout
-----------------
jQuery File upload progress bar with file size validation
-----------------
Polling system using PHP, Ajax and MySql
-----------------
Simple PHP File Cache
-----------------
SQL Injection Prevention Techniques
-----------------
Simple File Upload Script in PHP
-----------------
CSS Simple Menu Navigation Bar
-----------------
PHP User Authentication by IP Address
-----------------
Preventing Cross Site Request Forgeries(CSRF) in PHP
-----------------
How to generate QR Code in PHP
-----------------
Calculate the distance between two locations using PHP
-----------------
Simple way to send SMTP mail using Node.js
-----------------
Detect Mobile Devices in PHP
-----------------
Set and Get Cookies in PHP
-----------------
To check whether a year is a leap year or not in php
-----------------
PHP Server Side Form Validation
-----------------
Date Timestamp Formats in PHP
-----------------
Get Visitor\'s location and TimeZone
-----------------
Simple Show Hide Menu Navigation
-----------------
Convert MySQL to JSON using PHP
-----------------
PHP Programming Error Types
-----------------
PHP Sending HTML form data to an Email
-----------------
Driving route directions from source to destination using HTML5 and Javascript
-----------------
How to print specific part of a web page in javascript
-----------------
Google Street View API Example
-----------------
How to select/deselect all checkboxes using Javascript
-----------------
How to add google map on your website and display address on click marker
-----------------
PHP Getting Document of Remote Address
-----------------
PHP Connection and File Handling on FTP Server
-----------------
File Upload Validation in PHP
-----------------
R Plot Types
-----------------


Most Popular Blogs
Most in demand programming languages
Best mvc PHP frameworks in 2019
MariaDB vs MySQL
Most in demand NoSQL databases for 2019
Best AI Startups In India
Kotlin : Android App Development Choice
Kotlin vs Java which one is better
Top Android App Development Languages in 2019
Web Robots
Data Science Recruitment of Freshers - 2019


Interview Questions Answers
Basic PHP Interview
Advanced PHP Interview
MySQL Interview
Javascript Interview
HTML Interview
CSS Interview
Programming C Interview
Programming C++ Interview
Java Interview
Computer Networking Interview
NodeJS Interview
ExpressJS Interview
R Interview


Popular Tutorials
PHP Tutorial (Basic & Advance)
MySQL Tutorial & Exercise
MongoDB Tutorial
Python Tutorial & Exercise
Kotlin Tutorial & Exercise
R Programming Tutorial
HTML Tutorial
jQuery Tutorial
NodeJS Tutorial
ExpressJS Tutorial
Theory of Computation Tutorial
Data Structure Tutorial
Javascript Tutorial




General Knowledge

listen
listen
listen
listen
listen
listen
listen
listen
listen


Learn Popular Language

listen
listen
listen
listen
listen

Blogs

  • Jan 27

    Best AI Startups In India

    Artificial Intelligence is a process of making an intelligent computer machine that does tasks intelligently...

  • Jan 23

    Most in demand programming languages for 2019

    In this article, we have mentioned the analyzed results of the most in demand programming language for 2019...

  • Jan 15

    Web Robots

    Web robots is an internet robot or simply crawlers, or spiders and do not relate this with hardware robots...

  • Jan 12

    Most in demand NoSQL databases software for 2019

    In this article, we have mentioned the analyzed result of most in demand NoSQL database softwares for 2019...

  • Jan 10

    Kotlin : Android App Development Choice

    Kotlin is a general-purpose open-source programming language. It runs on the JVM and its syntax is much like Java...

Follow us

  • etutorialspoint facebook
  • etutorialspoint twitter
  • etutorialspoint linkedin
etutorialspoint youtube
About Us      Contact Us


  • eTutorialsPoint©Copyright 2016-2021. All Rights Reserved.