Home  >  Article  >  Backend Development  >  How to convert array into url parameters in php?

How to convert array into url parameters in php?

青灯夜游
青灯夜游Original
2020-08-25 11:23:406009browse

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;);

?>

Output result:

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

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 ]]] )

For example:

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

We can output $rescutl to get:

name=callback&value=test

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!

Statement:
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