1. 程式人生 > >http_build_query 字符串拼接

http_build_query 字符串拼接

off office function [1] mes all hello his nco

http_build_query 字符串拼接

產生一個urlencode之後的請求字符串。

1.將數組轉化成url問號(?)後的字符串

 1 <?php
 2   $date=array(
 3           ‘name‘=>‘tane‘,
 4           ‘sex‘ =>1,
 5           ‘job‘ => ‘officer‘
 6           ‘text‘ =>‘hello world‘
 7   );
 8   echo http_build_query($date);
 9   //輸出 name=tane&sex=1&job=officer&text=hello+world
10 ?>

2.http_build_query() 添加數字下標

$data = array(‘name‘, ‘tane‘, ‘sex‘,  ‘job‘ => ‘officer‘, ‘text‘ =>‘hello world‘);
   echo http_build_query($date,‘remot_‘);
   //輸出 remot_0=name&remote_1=tane&remot_2=sex&job=officer&text=hello+world

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‘<br>),‘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   註意:只有基礎數組中的數字下標元素“CEO”才獲取了前綴,其它數字下標元素(如   pastimes 下的元素)則不需要為了合法的變量名而加上前綴。   */ ?>

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

http_build_query 字符串拼接