Detailed explanation of the usage of http_build_query

小云云
Release: 2023-03-21 11:54:02
Original
6354 people have browsed it


http_build_query -- Generate the request string description string after url-encoded http_build_query (array formdata [, string numeric_prefix] )

Use to The associated (or subscripted) array generates a url-encoded request string. Parameters can be arrays or objects containing 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 numeric subscripts are used in the underlying array and the parameter is given, this parameter value will be used as the prefix of the numeric subscript elements in the underlying array. This is to allow or other The CGI program obtains legal variable names when it later decodes the data.


Example 1. http_build_query() Usage example

##$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=> ;'hypertext processor');
echo http_build_query($data);
/* Output:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
*/
?>

Example 2. http_build_query() Using numerical subscript elements

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

Example 3. http_build_query( ) Using complex arrays

<?php 
$data = array(&#39;user&#39;=>array(&#39;name&#39;=>&#39;Bob Smith&#39;, 
                            &#39;age&#39;=>47, 
                            &#39;sex&#39;=>&#39;M&#39;, 
                            &#39;dob&#39;=>&#39;5/12/1956&#39;), 
              &#39;pastimes&#39;=>array(&#39;golf&#39;, &#39;opera&#39;, &#39;poker&#39;, &#39;rap&#39;), 
              &#39;children&#39;=>array(&#39;bobby&#39;=>array(&#39;age&#39;=>12, 
                                               &#39;sex&#39;=>&#39;M&#39;), 
                                &#39;sally&#39;=>array(&#39;age&#39;=>8, 
                                               &#39;sex&#39;=>&#39;F&#39;)), 
              &#39;CEO&#39;); 
echo http_build_query($data, &#39;flags_&#39;); 
/* 输出:(为了可读性对其进行了折行) 
       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 = &#39;bar&#39;; 
    $this->baz = &#39;boom&#39;; 
   } 
} 
$data = new myClass(); 
echo http_build_query($data); 
/* 输出: 
       foo=bar&baz=boom 
*/ 
?>
Copy after login

Related recommendations:

http_build_query function that converts data into url in php

Trouble caused by the http_build_query function

php http_build_query implementation tutorial_PHP tutorial

The above is the detailed content of Detailed explanation of the usage of http_build_query. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!