PHP uses http_build_query () to construct a URL string. httpbuildquery_PHP tutorial

Source: Internet
Author: User
PHP uses http_build_query () to construct the URL string, httpbuildquery. PHP uses http_build_query () to construct a URL string. httpbuildquery this article describes how PHP uses http_build_query () to construct a URL string. For your reference, PHP uses http_build_query () to construct the URL string, httpbuildquery

This example describes how PHP uses http_build_query () to construct a URL string. We will share this with you for your reference. The details are as follows:

Simply put, http_build_query () is to convert an array into a url question mark? And urlencode is automatically processed.

Refer to the official explanation below:

Http_build_query

Http_build_query -- Generate the request string description after url-encoded string http_build_query (array formdata [, string numeric_prefix])

Generate a url-encoded request string using the given correlated (or subscript) array. The formdata parameter can be an array or an object containing attributes. A formdata array can be a simple one-dimensional structure or an array composed of arrays (which can contain other arrays in turn ). If the numeric subscript is used in the base array and the numeric_prefix parameter is provided, the value of this parameter is used as the prefix of the numeric subscript element in the base array. This is to allow PHP or other CGI programs to obtain valid variable names when decoding data later.

Let's take a look at some simple examples:

$data = array(    'foo'=>'bar',    'baz'=>'boom',    'site'=>'www.nowamagic.net',    'name'=>'nowa magic');echo http_build_query($data);/* output foo=bar&baz=boom&cow=milk&php=hypertext+processor*/

What if an array is composed of an index array and an associated array?

$data = array(    'foo',    'bar',    'site'=>'www.nowamagic.net',    'name'=>'nowa magic');echo http_build_query($data);/* output    0=foo&1=bar&site=www.nowamagic.net&name=nowa+magic*/

It automatically adds a Digital Index.

Http_build_query has another parameter. you can add a prefix to the numeric index. let's try again:

$data = array(    'foo',    'bar',    'site'=>'www.nowamagic.net',    'name'=>'nowa magic');echo http_build_query($data, "nm_");/* outputnm_0=foo&nm_1=bar&site=www.nowamagic.net&name=nowa+magic*/

What about complicated arrays? For example, a two-dimensional array or something.

$data = array(    'user'=>array('name'=>'Bob Smith',    'age'=>47,    'sex'=>'M',    'dob'=>'5/12/1956'),    'pastimes'=>array('golf', 'opera', 'poker', 'rap'),    'children'=>array('bobby'=>array('age'=>12,'sex'=>'M'),    'sally'=>array('age'=>8,'sex'=>'F')),    'CEO');

The output result is:

user%5Bname%5D=Bob+Smith&user%5Bage%5D=47&user%5Bsex%5D=M&user%5Bdob%5D=5%2F12%2F1956&pastimes%5B0%5D=golf&pastimes%5B1%5D=opera&pastimes%5B2%5D=poker&pastimes%5B3%5D=rap&children%5Bbobby%5D%5Bage%5D=12&children%5Bbobby%5D%5Bsex%5D=M&children%5Bsally%5D%5Bage%5D=8&children%5Bsally%5D%5Bsex%5D=F&0=CEO

For readability, it is broken:

user[name]=Bob+Smith&user[age]=47&user[sex]=M&user[dob]=5%1F12%1F1956&pastimes[0]=golf&pastimes[1]=opera&pastimes[2]=poker&pastimes[3]=rap&children[bobby][age]=12&children[bobby][sex]=M&children[sally][age]=8&children[sally][sex]=F&flags_0=CEO

Note: the prefix is obtained only when the numeric subscript element "CEO" in the basic array is used. Other numeric subscript elements (such as those in pastimes) do not need to be prefixed for valid variable names.

The object can be converted into a URL string, not just an array:

class myClass {  var $foo;  var $baz;  function myClass() {  $this->foo = 'bar';  $this->baz = 'boom';  }}$data = new myClass();echo http_build_query($data);

At the end, I will mention several more functions, which you may need to know when searching for http_build_query:

1. parse_str: Set a url? The following parameters are converted into an array, array parse_str (url, arr ).

2. parse_url: parses a complete url into an array, array parse_url (string url ).

3. http_build_query: briefly explain how to convert an array into a url? The parameter string that follows will automatically perform urlencode processing, string http_build_query (array formdata [, string numeric_prefix]), followed by a subscript that does not specify a key or a key as a number in the array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.