Detailed explanation of PHP function http_build_query
- 2021-07-13 04:52:11
- OfStack
What is http_build_query?
Generates a request string through URL-encode using the given array of associations (or subscripts). The parameter formdata can be an array or an object containing properties. An formdata array can be a simple 1-dimensional structure or an array of arrays (which in turn can contain other arrays). If a numeric subscript is used in the underlying array and the numeric_prefix parameter is given, the value of this parameter will prefix the numeric subscript element in the underlying array. This is for PHP or other CGI programs to get legal variable names when decoding the data later
http_build_query can be used in many ways to pass in not only associative arrays, but also indexed arrays, even multidimensional arrays and objects.
How to use http_build_query?
string http_build_query ( array $formdata [, string $numeric_prefix ] )
Pass in a 1-dimensional associative array
Array
(
[name] => lizhong
[age] => 18
)
name=lizhong&age=18
Pass in a 1-dimensional index array
Array
(
[0] => lizhong
[1] => 18
)
0=lizhong&1=18
Pass in a 2-D array
Array
(
[a] => Array
(
[a] => a
[b] => b
)
[c] => c
)
a%5Ba%5D=a&a%5Bb%5D=b&c=c
Incoming object
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:
a=a&b=b
Because $c is a private variable, the $c member is not accessible, so only a and b are output. And the function in the object will not be printed!