Table of Contents
http_build_query
说明
Home Backend Development PHP Tutorial php http_build_query 拼凑数组

php http_build_query 拼凑数组

Jun 13, 2016 pm 01:19 PM
array build http query url

php http_build_query 拼接数组

?

http_build_query

(PHP 5)

http_build_query?―?生成 URL-encode 之后的请求字符串

说明

string?http_build_query?(?array?$formdata?[,?string?$numeric_prefix?] )

使用给出的关联(或下标)数组生成一个经过 URL-encode 的请求字符串。参数?formdata?可以是数组或包含属性的对象。一个?formdata?数组可以是简单的一维结构,也可以是由数组组成的数组(其依次可以包含其它数组)。如果在基础数组中使用了数字下标同时给出了?numeric_prefix?参数,此参数值将会作为基础数组中的数字下标元素的前缀。这是为了让 PHP 或其它 CGI 程序在稍后对数据进行解码时获取合法的变量名。

Note:

用?arg_separator.output?来分隔参数。

Example #1?http_build_query()?使用示例

<span style="line-height: 21px; color: #000000;"><span style="line-height: 21px;"><span style="line-height: 21px; color: #0000bb;"><?php <br>$data?</span><span style="line-height: 21px; color: #007700;">=?array(</span><span style="line-height: 21px; color: #dd0000;">'foo'</span><span style="line-height: 21px; color: #007700;">=></span><span style="line-height: 21px; color: #dd0000;">'bar'</span></span><span style="line-height: 21px;"><span style="line-height: 21px; color: #007700;">,<br>??????????????</span><span style="line-height: 21px; color: #dd0000;">'baz'</span><span style="line-height: 21px; color: #007700;">=></span><span style="line-height: 21px; color: #dd0000;">'boom'</span></span><span style="line-height: 21px;"><span style="line-height: 21px; color: #007700;">,<br>??????????????</span><span style="line-height: 21px; color: #dd0000;">'cow'</span><span style="line-height: 21px; color: #007700;">=></span><span style="line-height: 21px; color: #dd0000;">'milk'</span></span><span style="line-height: 21px;"><span style="line-height: 21px; color: #007700;">,<br>??????????????</span><span style="line-height: 21px; color: #dd0000;">'php'</span><span style="line-height: 21px; color: #007700;">=></span><span style="line-height: 21px; color: #dd0000;">'hypertext?processor'</span></span><span style="line-height: 21px;"><span style="line-height: 21px; color: #007700;">);<br>??????????????<br>echo?</span><span style="line-height: 21px; color: #0000bb;">http_build_query</span><span style="line-height: 21px; color: #007700;">(</span><span style="line-height: 21px; color: #0000bb;">$data</span><span style="line-height: 21px; color: #007700;">);?</span></span><span style="line-height: 21px;"><span style="line-height: 21px; color: #ff8000;">//?foo=bar&baz=boom&cow=milk&php=hypertext+processor<br></span><span style="line-height: 21px; color: #0000bb;">?></span></span></span>

Example #2?http_build_query()?使用数字下标的元素

<span style="line-height: 21px; color: #000000;"><span style="line-height: 21px;"><span style="line-height: 21px; color: #0000bb;"><?php <br>$data?</span><span style="line-height: 21px; color: #007700;">=?array(</span><span style="line-height: 21px; color: #dd0000;">'foo'</span><span style="line-height: 21px; color: #007700;">,?</span><span style="line-height: 21px; color: #dd0000;">'bar'</span><span style="line-height: 21px; color: #007700;">,?</span><span style="line-height: 21px; color: #dd0000;">'baz'</span><span style="line-height: 21px; color: #007700;">,?</span><span style="line-height: 21px; color: #dd0000;">'boom'</span><span style="line-height: 21px; color: #007700;">,?</span><span style="line-height: 21px; color: #dd0000;">'cow'?</span><span style="line-height: 21px; color: #007700;">=>?</span><span style="line-height: 21px; color: #dd0000;">'milk'</span><span style="line-height: 21px; color: #007700;">,?</span><span style="line-height: 21px; color: #dd0000;">'php'?</span><span style="line-height: 21px; color: #007700;">=></span><span style="line-height: 21px; color: #dd0000;">'hypertext?processor'</span></span><span style="line-height: 21px;"><span style="line-height: 21px; color: #007700;">);<br>??????????????<br>echo?</span><span style="line-height: 21px; color: #0000bb;">http_build_query</span><span style="line-height: 21px; color: #007700;">(</span><span style="line-height: 21px; color: #0000bb;">$data</span></span><span style="line-height: 21px; color: #007700;"><span style="line-height: 21px;">);<br></span></span><span style="line-height: 21px;"><span style="line-height: 21px; color: #ff8000;"><br>?<br></span><span style="line-height: 21px; color: #007700;">echo?</span><span style="line-height: 21px; color: #0000bb;">http_build_query</span><span style="line-height: 21px; color: #007700;">(</span><span style="line-height: 21px; color: #0000bb;">$data</span><span style="line-height: 21px; color: #007700;">,?</span><span style="line-height: 21px; color: #dd0000;">'myvar_'</span></span><span style="line-height: 21px; color: #007700;"><span style="line-height: 21px;">);<br></span></span><span style="line-height: 21px;"><span style="line-height: 21px; color: #ff8000;"><br></span><span style="line-height: 21px; color: #0000bb;">?></span></span></span>


=======================================================================================


简单的理解这两个函数

parse_str就是将一个url ?后面的参数转换成一个数组

array parse_str(url,arr)


parse_url是将一个完整的url解析成数组

array parse_url(string url)


http_build_query就是将一个数组转换成url ?后面的参数字符串,会自动进行urlencode处理

string http_build_query ( array formdata [, string numeric_prefix])

后面的给数组中没有指定键或者键为数字的加下标

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does http status code 520 mean? What does http status code 520 mean? Oct 13, 2023 pm 03:11 PM

HTTP status code 520 means that the server encountered an unknown error while processing the request and cannot provide more specific information. Used to indicate that an unknown error occurred when the server was processing the request, which may be caused by server configuration problems, network problems, or other unknown reasons. This is usually caused by server configuration issues, network issues, server overload, or coding errors. If you encounter a status code 520 error, it is best to contact the website administrator or technical support team for more information and assistance.

Why NameResolutionError(self.host, self, e) from e and how to solve it Why NameResolutionError(self.host, self, e) from e and how to solve it Mar 01, 2024 pm 01:20 PM

The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

What is the difference between html and url What is the difference between html and url Mar 06, 2024 pm 03:06 PM

Differences: 1. Different definitions, url is a uniform resource locator, and html is a hypertext markup language; 2. There can be many urls in an html, but only one html page can exist in a url; 3. html refers to is a web page, and url refers to the website address.

Microsoft Win11 24H2 Build 26100 has found multiple bugs and installation is not recommended Microsoft Win11 24H2 Build 26100 has found multiple bugs and installation is not recommended Apr 07, 2024 pm 09:22 PM

The latest news from this site on April 7 is that multiple bugs have been discovered in Microsoft Win1124H2Build26100, and it is temporarily not recommended to install it on commonly used devices. Windows 1124H2 is still in a very unstable stage. Although Microsoft will gradually fix the problem through monthly cumulative updates, it may take some time before this version is officially released and brings a stable experience to everyone. If any user has installed the 24H2 version image and encountered problems, it is recommended that the user reinstall it. It is now relatively stable. ZacBowden, encountered a Blue Screen of Death when trying to reset Windows 1124H2Build26100 version, and the "Reset this PC" function was supposed to fix system problems, but instead

Understand common application scenarios of web page redirection and understand the HTTP 301 status code Understand common application scenarios of web page redirection and understand the HTTP 301 status code Feb 18, 2024 pm 08:41 PM

Understand the meaning of HTTP 301 status code: common application scenarios of web page redirection. With the rapid development of the Internet, people's requirements for web page interaction are becoming higher and higher. In the field of web design, web page redirection is a common and important technology, implemented through the HTTP 301 status code. This article will explore the meaning of HTTP 301 status code and common application scenarios in web page redirection. HTTP301 status code refers to permanent redirect (PermanentRedirect). When the server receives the client's

Common network communication and security problems and solutions in C# Common network communication and security problems and solutions in C# Oct 09, 2023 pm 09:21 PM

Common network communication and security problems and solutions in C# In today's Internet era, network communication has become an indispensable part of software development. In C#, we usually encounter some network communication problems, such as data transmission security, network connection stability, etc. This article will discuss in detail common network communication and security issues in C# and provide corresponding solutions and code examples. 1. Network communication problems Network connection interruption: During the network communication process, the network connection may be interrupted, which may cause

HTTP 200 OK: Understand the meaning and purpose of a successful response HTTP 200 OK: Understand the meaning and purpose of a successful response Dec 26, 2023 am 10:25 AM

HTTP Status Code 200: Explore the Meaning and Purpose of Successful Responses HTTP status codes are numeric codes used to indicate the status of a server's response. Among them, status code 200 indicates that the request has been successfully processed by the server. This article will explore the specific meaning and use of HTTP status code 200. First, let us understand the classification of HTTP status codes. Status codes are divided into five categories, namely 1xx, 2xx, 3xx, 4xx and 5xx. Among them, 2xx indicates a successful response. And 200 is the most common status code in 2xx

http request 415 error solution http request 415 error solution Nov 14, 2023 am 10:49 AM

Solution: 1. Check the Content-Type in the request header; 2. Check the data format in the request body; 3. Use the appropriate encoding format; 4. Use the appropriate request method; 5. Check the server-side support.

See all articles