http_build_query

(PHP 5, PHP 7)

http_build_query锟斤拷锟斤拷 URL-encode 之锟斤拷锟斤拷锟斤拷锟斤拷址锟斤拷锟�

说锟斤拷

http_build_query ( mixed $query_data [, string $numeric_prefix [, string $arg_separator [, int $enc_type = PHP_QUERY_RFC1738 ]]] ) : string

使锟矫革拷锟斤拷锟侥癸拷锟斤拷锟斤拷锟斤拷锟铰标)锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷 URL-encode 锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷

锟斤拷锟斤拷

query_data

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟皆的讹拷锟斤拷

一锟斤拷 query_data 锟斤拷锟斤拷锟斤拷锟斤拷羌虻サ锟揭晃拷峁癸拷锟揭诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟缴碉拷锟斤拷锟介(锟斤拷锟斤拷锟轿匡拷锟皆帮拷锟斤拷锟斤拷锟斤拷锟斤拷锟介)锟斤拷

锟斤拷锟� query_data 锟斤拷一锟斤拷锟斤拷锟斤拷只锟斤拷 public 锟斤拷锟斤拷锟皆伙拷锟斤拷锟斤拷锟斤拷锟�

numeric_prefix

锟斤拷锟斤拷诨锟斤拷锟斤拷锟斤拷锟斤拷锟绞癸拷锟斤拷锟斤拷锟斤拷锟斤拷卤锟酵憋拷锟斤拷锟斤拷烁貌锟斤拷锟斤拷锟斤拷瞬锟斤拷锟街碉拷锟斤拷锟斤拷锟轿拷锟斤拷锟斤拷锟斤拷锟斤拷械锟斤拷锟斤拷锟斤拷卤锟皆拷氐锟角白猴拷锟�

锟斤拷锟斤拷为锟斤拷锟斤拷 PHP 锟斤拷锟斤拷锟斤拷 CGI 锟斤拷锟斤拷锟斤拷锟皆猴拷锟斤拷锟斤拷萁锟斤拷薪锟斤拷锟绞憋拷锟饺★拷戏锟斤拷谋锟斤拷锟斤拷锟斤拷锟�

arg_separator

锟斤拷锟斤拷指锟斤拷锟斤拷使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 arg_separator.output 锟斤拷锟街革拷锟斤拷锟斤拷锟斤拷

enc_type

默锟斤拷使锟斤拷 PHP_QUERY_RFC1738锟斤拷

锟斤拷锟� enc_type 锟斤拷 PHP_QUERY_RFC1738锟斤拷锟斤拷锟斤拷虢拷锟斤拷锟� » RFC 1738 锟斤拷准锟斤拷 application/x-www-form-urlencoded 媒锟斤拷锟斤拷锟酵斤拷锟叫憋拷锟诫,锟秸革拷岜伙拷锟斤拷锟缴加号o拷+锟斤拷锟斤拷

锟斤拷锟� enc_type 锟斤拷 PHP_QUERY_RFC3986锟斤拷锟斤拷锟斤拷锟斤拷 » RFC 3986 锟斤拷锟诫,锟秸革拷岜伙拷俜趾疟锟斤拷耄�%20锟斤拷锟斤拷

锟斤拷锟斤拷值

锟斤拷锟斤拷一锟斤拷 URL 锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
5.4.0 锟斤拷锟斤拷锟斤拷 enc_type 锟斤拷锟斤拷锟斤拷
5.1.3 锟斤拷锟斤拷锟斤拷也锟结被转锟藉。
5.1.2 锟斤拷锟斤拷锟剿诧拷锟斤拷 arg_separator锟斤拷

锟斤拷锟斤拷

Example #1 http_build_query() 使锟斤拷示锟斤拷

<?php
$data 
= array('foo'=>'bar',
              
'baz'=>'boom',
              
'cow'=>'milk',
              
'php'=>'hypertext processor');

echo 
http_build_query($data) . "\n";
echo 
http_build_query($data'''&amp;');

?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&amp;baz=boom&amp;cow=milk&amp;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) . "\n";
echo 
http_build_query($data'myvar_');
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

0=foo&1=bar&2=baz&3=boom&cow=milk&php=hypertext+processor
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_');
?>

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为锟剿可讹拷锟皆o拷锟斤拷锟窖撅拷锟斤拷锟斤拷锟剿o拷

user%5Bname%5D=Bob+Smith&user%5Bage%5D=47&user%5Bsex%5D=M&
user%5Bdob%5D=5%2F12%2F1956&pastimes%5B0%5D=golf&pastimes%5B1%5D=opera&
pastimes%5B2%5D=poker&pastimes%5B3%5D=rap&children%5Bbobby%5D%5Bage%5D=12&
children%5Bbobby%5D%5Bsex%5D=M&children%5Bsally%5D%5Bage%5D=8&
children%5Bsally%5D%5Bsex%5D=F&flags_0=CEO

Note:

只锟叫伙拷锟斤拷锟斤拷锟斤拷锟叫碉拷锟斤拷锟斤拷锟铰憋拷元锟斤拷"CEO"锟脚伙拷取锟斤拷前缀锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟铰憋拷元锟截o拷锟斤拷 pastimes 锟铰碉拷元锟截o拷锟斤拷锟斤拷要为锟剿合凤拷锟侥憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷前缀锟斤拷

Example #4 http_build_query() 使锟矫讹拷锟斤拷

<?php
class parentClass {
    public    
$pub      'publicParent';
    protected 
$prot     'protectedParent';
    private   
$priv     'privateParent';
    public    
$pub_bar  Null;
    protected 
$prot_bar Null;
    private   
$priv_bar Null;

    public function 
__construct(){
        
$this->pub_bar  = new childClass();
        
$this->prot_bar = new childClass();
        
$this->priv_bar = new childClass();
    }
}

class 
childClass {
    public    
$pub  'publicChild';
    protected 
$prot 'protectedChild';
    private   
$priv 'privateChild';
}

$parent = new parentClass();

echo 
http_build_query($parent);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

pub=publicParent&pub_bar%5Bpub%5D=publicChild

锟轿硷拷

  • parse_str() - 锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟缴讹拷锟斤拷锟斤拷锟�
  • parse_url() - 锟斤拷锟斤拷 URL锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷刹锟斤拷锟�
  • urlencode() - 锟斤拷锟斤拷 URL 锟街凤拷锟斤拷
  • array_walk() - 使锟斤拷锟矫伙拷锟皆讹拷锟藉函锟斤拷锟斤拷锟斤拷锟斤拷锟叫碉拷每锟斤拷元锟斤拷锟斤拷锟截碉拷锟斤拷锟斤拷