How to convert array into url parameters in php?

青灯夜游
Release: 2023-03-05 08:18:01
Original
6053 people have browsed it

In PHP, you can use the built-in "http_build_query()" function to convert the array into url parameters. The "http_build_query()" function was added to php5. Its function is to convert an array or object into a url parameter and generate a "URL-encoded" request string.

How to convert array into url parameters in php?

Recommended: "PHP Video Tutorial"

php uses the http_build_query() function to convert the array into url parameter

<?php
$data = array(
    &#39;foo&#39; => &#39;bar&#39;,
    &#39;baz&#39; => &#39;boom&#39;,
    &#39;cow&#39; => &#39;milk&#39;,
    &#39;php&#39; => &#39;hypertext processor&#39;
);

echo http_build_query($data) . "\n";
echo http_build_query($data, &#39;&#39;, &#39;&amp;&#39;);

?>
Copy after login

Output result:

foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&amp;baz=boom&amp;cow=milk&amp;php=hypertext+processor
Copy after login

http_build_query() function introduction

The function of http_build_query() function is to use the given The associative (or subscripted) array generates a URL-encoded request string.

Writing format:

http_build_query ( mixed $query_data [, string $numeric_prefix [, string $arg_separator [, int $enc_type = PHP_QUERY_RFC1738 ]]] )
Copy after login

For example:

$data = array("name"=>"callback" , "value"=>"test");
$rescult = http_build_query($data);
Copy after login

We can output $rescutl to get:

name=callback&value=test
Copy after login

What is the use of this? This is To simulate http requests, pass the obtained data data through the function URL-encode, which is usually used in callbacks.

The above is the detailed content of How to convert array into url parameters in php?. 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