PHP http_build_query function
Like this blog? Consider exploring one of our sponsored banner ads...
http_build_query
(PHP 5)
http_build_query — Generate URL-encoded query stringDescriptionstring http_build_query ( array formdata [, string numeric_prefix [, string arg_separator]] )
Generates a URL-encoded query string from the associative (or indexed)
array provided.
formdata
May be an array or object containing properties.
The array form may be a simple one-dimensional structure, or an array
of arrays (who in turn may contain other arrays).
If numeric indices are used in the base array and this parameter is
provided, it will be prepended to the numeric index for elements in
the base array only.
This is meant to allow for legal variable names when the data is
decoded by PHP or another CGI application later on.
arg_separator.output
is used to separate arguments, unless this parameter is specified,
and is then used.
Return Values
Returns a URL-encoded string.
VersionDescription5.1.2 The arg_separator parameter was added.
Examples
Example 1. Simple usage of http_build_query()
$data = array('foo'=>'bar', 'baz'=>'boom', 'cow'=>'milk', 'php'=>'hypertext processor'); echo http_build_query($data); // foo=bar&baz=boom&cow=milk&php=hypertext+processor echo http_build_query($data, '', '&'); // foo=bar&baz=boom&cow=milk&php=hypertext+processor
Example 2. http_build_query() with numerically index elements.
$data = array('foo', 'bar', 'baz', 'boom', 'cow' => 'milk', 'php' =>'hypertext processor'); echo http_build_query($data) . "\n"; echo http_build_query($data, 'myvar_');
The above example will output:
0=foo&1=bar&2=baz&3=boom&cow=milk&php=hypertext+processormyvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk&php=hypertext+processor
Example 3. http_build_query() with complex arrays
$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'); echo http_build_query($data, 'flags_');
this will output : (word wrapped for readability)
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:
Only the numerically indexed element in the base array “CEO” received a
prefix. The other numeric indices, found under pastimes, do not
require a string prefix to be legal variable names.
Example 4. Using http_build_query() with an object
class myClass { var $foo; var $baz; function myClass() { $this->foo = 'bar'; $this->baz = 'boom'; } } $data = new myClass(); echo http_build_query($data); // foo=bar&baz=boom
See Also
parse_str()parse_url()urlencode()array_walk()
About this entry
You’re currently reading “PHP http_build_query function,” an entry on BRADINO
- Published:
- 2.26.07 / 1am
- Category:
- PHP Functions
- Tags:
No comments
Jump to comment form | comments rss [?] | trackback uri [?]