http_build_query 生成 URL-encode 之后的请求字符串 php函数


http_build_query

(PHP 5)

http_build_query生成 URL-encode 之后的请求字符串

说明

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

使用给出的关联(或下标)数组生成一个经过 URL-encode 的请求字符串。参数 formdata 可以是数组或包含属性的对象。一个 formdata 数组可以是简单的一维结构,也可以是由数组组成的数组(其依次可以包含其它数组)。如果在基础数组中使用了数字下标同时给出了 numeric_prefix 参数,此参数值将会作为基础数组中的数字下标元素的前缀。这是为了让 PHP 或其它 CGI 程序在稍后对数据进行解码时获取合法的变量名。

Note:

用 arg_separator.output 来分隔参数。

Example #1 http_build_query() 使用示例

<?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() 使用数字下标的元素

<?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() 使用复杂的数组

<?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_');
?>

本例将输出(为可读性进行了折行):

      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:

只有基础数组中的数字下标元素“CEO”才获取了前缀,其它数字下标元素(如 pastimes 下的元素)则不需要为了合法的变量名而加上前缀。

Example #4 http_build_query() 使用对象

<?php
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
?>

参见 parse_str()parse_url()urlencode()array_walk()



相关阅读:
http_response_code Get or Set the HTTP response code
hypot 计算一直角三角形的斜边长度
ibase_add_user Add a user to a security database (only for IB6 or later)
ibase_affected_rows Return the number of rows that were affected by the previous query
ibase_backup Initiates a backup task in the service manager and returns immediately
ibase_blob_add Add data into a newly created blob
ibase_blob_cancel Cancel creating blob
ibase_blob_close Close blob
ibase_blob_create Create a new blob for adding data
ibase_blob_echo Output blob contents to browser
ibase_blob_get Get len bytes data from open blob
ibase_blob_import Create blob, copy file in it, and close it
ibase_blob_info Return blob length and other useful info
ibase_blob_open Open blob for retrieving data parts
快速导航

Copyright © 2016 phpStudy |