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!