PHP function http_build_query usage details

Source: Internet
Author: User
This article mainly introduces how to use the PHP function http_build_query. It provides examples for passing in one-dimensional correlated arrays, one-dimensional index arrays, two-dimensional arrays, and input objects. For more information, see What is http_build_query?

Generate a URL-encode 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.
Http_build_query can be used in multiple ways. it can be used not only to input an associated array, but also to input an index array, or even multi-dimensional arrays and objects.

How to use http_build_query?

The code is as follows:


String http_build_query (array $ formdata [, string $ numeric_prefix])

Input a one-dimensional join array

The code is as follows:


Array
(
[Name] => lizhong
[Age] => 18
)
Name = lizhong & age = 18

Input a one-dimensional index array

The code is as follows:


Array
(
[0] => lizhong
[1] => 18
)
0 = lizhong & 1 = 18


Input two-dimensional array

The code is as follows:


Array
(
[A] => Array
(
[A] =>
[B] => B
)

[C] => c
)

A % 5Ba % 5D = a & a % 5Bb % 5D = B & c = c


Input object

The code is as follows:


Class Obj {
Public $ a = 'a ';
Public $ B = 'B ';
Private $ c = 'C ';
Public function func (){
Return;
}
}


$ Obj = new Obj ();

$ Str = http_build_query ($ obj );

Echo $ str;


Output:

The code is as follows:


A = a & B = B


Because $ c is a private variable, $ c members cannot be accessed, so only a and B are output. The functions in the object will not be printed!

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.