A detailed explanation of http_build_query usage

Source: Internet
Author: User
Tags php class

Http_build_query--The request string after generating url-encoded describes string http_build_query (Array formdata [, String Numeric_prefix])

Generates an url-encoded request string using the given array of associations (or subscripts). Parameters can be arrays or objects that contain properties. An array can be a simple one-dimensional structure, or it can be an array of arrays (which in turn can contain other arrays). If a numeric subscript is used in the underlying array and a parameter is given, the value of this parameter will be prefixed with the number subscript element in the underlying array. This is to allow or other CGI programs to obtain a valid variable name when decoding the data later.


Example 1. http_build_query () using the example

<?php
$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
*/
?>

Example 2. http_build_query () elements using a numeric subscript

<?php
$data = array('foo', 'bar', 'baz', 'boom', 'cow' => 'milk', 'php' =>'hypertext processor');
echo http_build_query($data);
/* 输出:
0=foo&1=bar&2=baz&3=boom&cow=milk&php=hypertext+processor
*/
echo http_build_query($data, 'myvar_');
/* 输出:
myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk&php=hypertext+processor
*/
?>

Example 3. http_build_query () using a complex array

<?php $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_ ');        /* Output: (Wrapping it for readability) 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: Only the number in the underlying array The word subscript element "CEO" only obtains the prefix, other digital subscript elements (such as the elements under pastimes) do not need toA valid variable name plus a prefix. */?> Example 4.    Http_build_query () uses the object <?php class MyClass {var $foo;    var $baz;     function MyClass () {$this->foo = ' bar ';    $this->baz = ' boom '; }} $data = new MyClass (); echo Http_build_query ($data); /* Output: Foo=bar&baz=boom */?>

Related recommendations:

Http_build_query function for converting data into URLs in PHP

The Http_build_query function brings the puzzle

PHP http_build_query Implementation Tutorial _php Tutorial

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.