當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP http_build_query()用法及代碼示例

http_build_query()函數是PHP中的內置函數,用於從關聯(或索引)數組生成URL編碼的查詢字符串。

用法:

string http_build_query( $query_data, $numeric_prefix, $arg_separator, $enc_type = PHP_QUERY_RFC1738 )

參數:該函數接受上述和以下所述的四個參數:


  • $query_data:此參數保存包含以下屬性的數組或對象:
    • 它可以是一維數組或多維數組。
    • 如果$query_data是對象,則僅將公共屬性合並到結果中。
  • $numeric_prefix:如果在基本數組中使用了數字索引,則使用此參數,它將僅在基本數組中元素的數字索引之前。
  • $arg_separator:它用於分隔參數,但可以通過指定此參數來覆蓋它。
  • $enc_type:其默認值為PHP_QUERY_RFC1738。

返回值:它返回URL編碼的字符串。

以下示例程序旨在說明PHP中的http_build_query()函數:

程序1:

<?php 
$info = array( 
    'sudo' => 'placement', 
    'CPP' => 'course', 
    'FORK' => 'C', 
); 
  
echo http_build_query($info) . "#"; 
echo http_build_query($info, '', '&'); 
  
?>
輸出:
sudo=placement&CPP=course&FORK=C#sudo=placement&CPP=course&FORK=C

程序2:

<?php 
$info = array('geeks', 'gfg' => 'sudo', 'placement' => 'hypertext processor'); 
  
echo http_build_query($info) . "$"; 
echo http_build_query($info, 'myvar_'); 
?>
輸出:
0=geeks&gfg=sudo&placement=hypertext+processor$myvar_0=geeks&gfg=sudo&placement=hypertext+processor

參考: http://docs.php.net/manual/da/function.http-build-query.php



相關用法


注:本文由純淨天空篩選整理自Code_Mech大神的英文原創作品 PHP | http_build_query() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。