Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 1457

php-manual-zh-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>http_build_query</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="PHP 手册"
HREF="index.html"><LINK
REL="UP"
TITLE="URL 函数"
HREF="ref.url.html"><LINK
REL="PREVIOUS"
TITLE="get_meta_tags"
HREF="function.get-meta-tags.html"><LINK
REL="NEXT"
TITLE="parse_url"
HREF="function.parse-url.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="refentry"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP 手册</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.get-meta-tags.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.parse-url.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.http-build-query"
></A
>http_build_query</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN235763"
></A
><P
>    (PHP 5)</P
>http_build_query&nbsp;--&nbsp;生成 URL-encode 之后的请求字符串</DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN235766"
></A
><H2
>说明</H2
>string <B
CLASS="methodname"
>http_build_query</B
> ( array formdata [, string numeric_prefix] )<BR
></BR
><P
>&#13;   使用给出的关联(或下标)数组生成一个经过
   URL-encode 的请求字符串。参数
   <CODE
CLASS="parameter"
>formdata</CODE
> 可以是数组或包含属性的对象。一个
   <CODE
CLASS="parameter"
>formdata</CODE
> 
   数组可以是简单的一维结构,也可以是由数组组成的数组(其依次可以包含其它数组)。如果在基础数组中使用了数字下标同时给出了
   <CODE
CLASS="parameter"
>numeric_prefix</CODE
> 参数,此参数值将会作为基础数组中的数字下标元素的前缀。这是为了让
   PHP 或其它 CGI
   程序在稍后对数据进行解码时获取合法的变量名。
  </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
    用 <A
HREF="ini.core.html#ini.arg-separator.output"
>arg_separator.output</A
> 来分隔参数。
   </P
></BLOCKQUOTE
></DIV
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN235784"
></A
><P
><B
>例 1. <B
CLASS="function"
>http_build_query()</B
> 使用示例</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$data </font><font color="#007700">= array(</font><font color="#DD0000">'foo'</font><font color="#007700">=&gt;</font><font color="#DD0000">'bar'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'baz'</font><font color="#007700">=&gt;</font><font color="#DD0000">'boom'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'cow'</font><font color="#007700">=&gt;</font><font color="#DD0000">'milk'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'php'</font><font color="#007700">=&gt;</font><font color="#DD0000">'hypertext processor'</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />echo </font><font color="#0000BB">http_build_query</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">); </font><font color="#FF8000">// foo=bar&amp;baz=boom&amp;cow=milk&amp;php=hypertext+processor<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN235788"
></A
><P
><B
>例 2. <B
CLASS="function"
>http_build_query()</B
> 使用数字下标的元素</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$data </font><font color="#007700">= array(</font><font color="#DD0000">'foo'</font><font color="#007700">, </font><font color="#DD0000">'bar'</font><font color="#007700">, </font><font color="#DD0000">'baz'</font><font color="#007700">, </font><font color="#DD0000">'boom'</font><font color="#007700">, </font><font color="#DD0000">'cow' </font><font color="#007700">=&gt; </font><font color="#DD0000">'milk'</font><font color="#007700">, </font><font color="#DD0000">'php' </font><font color="#007700">=&gt;</font><font color="#DD0000">'hypertext processor'</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />echo </font><font color="#0000BB">http_build_query</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">);<br /></font><font color="#FF8000">/* 输出:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0=foo&amp;1=bar&amp;2=baz&amp;3=boom&amp;cow=milk&amp;php=hypertext+processor<br /> */<br /> <br /></font><font color="#007700">echo </font><font color="#0000BB">http_build_query</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">, </font><font color="#DD0000">'myvar_'</font><font color="#007700">);<br /></font><font color="#FF8000">/* 输出:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myvar_0=foo&amp;myvar_1=bar&amp;myvar_2=baz&amp;myvar_3=boom&amp;cow=milk&amp;php=hypertext+processor<br /> */<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN235792"
></A
><P
><B
>例 3. <B
CLASS="function"
>http_build_query()</B
> 使用复杂的数组</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$data </font><font color="#007700">= array(</font><font color="#DD0000">'user'</font><font color="#007700">=&gt;array(</font><font color="#DD0000">'name'</font><font color="#007700">=&gt;</font><font color="#DD0000">'Bob Smith'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'age'</font><font color="#007700">=&gt;</font><font color="#0000BB">47</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'sex'</font><font color="#007700">=&gt;</font><font color="#DD0000">'M'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'dob'</font><font color="#007700">=&gt;</font><font color="#DD0000">'5/12/1956'</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'pastimes'</font><font color="#007700">=&gt;array(</font><font color="#DD0000">'golf'</font><font color="#007700">, </font><font color="#DD0000">'opera'</font><font color="#007700">, </font><font color="#DD0000">'poker'</font><font color="#007700">, </font><font color="#DD0000">'rap'</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'children'</font><font color="#007700">=&gt;array(</font><font color="#DD0000">'bobby'</font><font color="#007700">=&gt;array(</font><font color="#DD0000">'age'</font><font color="#007700">=&gt;</font><font color="#0000BB">12</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'sex'</font><font color="#007700">=&gt;</font><font color="#DD0000">'M'</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'sally'</font><font color="#007700">=&gt;array(</font><font color="#DD0000">'age'</font><font color="#007700">=&gt;</font><font color="#0000BB">8</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'sex'</font><font color="#007700">=&gt;</font><font color="#DD0000">'F'</font><font color="#007700">)),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'CEO'</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />echo </font><font color="#0000BB">http_build_query</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">, </font><font color="#DD0000">'flags_'</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;    本例将输出(为可读性进行了折行):
   </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>user[name]=Bob+Smith&#38;user[age]=47&#38;user[sex]=M&#38;user[dob]=5%1F12%1F1956&#38;
      pastimes[0]=golf&#38;pastimes[1]=opera&#38;pastimes[2]=poker&#38;pastimes[3]=rap&#38;
      children[bobby][age]=12&#38;children[bobby][sex]=M&#38;children[sally][age]=8&#38;
      children[sally][sex]=F&#38;flags_0=CEO</PRE
></TD
></TR
></TABLE
><P
>&#13;    <DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
      只有基础数组中的数字下标元素“CEO”才获取了前缀,其它数字下标元素(如
      pastimes 下的元素)则不需要为了合法的变量名而加上前缀。
     </P
></BLOCKQUOTE
></DIV
>
   </P
></DIV
></TD
></TR
></TABLE
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN235801"
></A
><P
><B
>例 4. <B
CLASS="function"
>http_build_query()</B
> 使用对象</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">class </font><font color="#0000BB">myClass </font><font color="#007700">{<br />&nbsp;&nbsp;var </font><font color="#0000BB">$foo</font><font color="#007700">;<br />&nbsp;&nbsp;var </font><font color="#0000BB">$baz</font><font color="#007700">;<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;function </font><font color="#0000BB">myClass</font><font color="#007700">()<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">foo </font><font color="#007700">= </font><font color="#DD0000">'bar'</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">baz </font><font color="#007700">= </font><font color="#DD0000">'boom'</font><font color="#007700">;<br />&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#0000BB">$data </font><font color="#007700">= new </font><font color="#0000BB">myClass</font><font color="#007700">();<br /><br />echo </font><font color="#0000BB">http_build_query</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">); </font><font color="#FF8000">// foo=bar&amp;baz=boom<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;   参见 <A
HREF="function.parse-str.html"
><B
CLASS="function"
>parse_str()</B
></A
>,<A
HREF="function.parse-url.html"
><B
CLASS="function"
>parse_url()</B
></A
>,<A
HREF="function.urlencode.html"
><B
CLASS="function"
>urlencode()</B
></A
>
   和 <A
HREF="function.array-walk.html"
><B
CLASS="function"
>array_walk()</B
></A
>。
  </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.get-meta-tags.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>起始页</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.parse-url.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>get_meta_tags</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.url.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>parse_url</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>