Home > Article > Backend Development > How to convert array to URL parameters in PHP
How to convert an array into a URL parameter in PHP
1. Use the PHP built-in function "http_build_query()" to convert a string into a URL parameter;
Usage example:
<?php $data = array( 'foo' => 'bar', 'baz' => 'boom', 'cow' => 'milk', 'php' => 'hypertext processor' ); echo http_build_query($data) . "\n"; echo http_build_query($data, '', '&'); ?>
Output result:
foo=bar&baz=boom&cow=milk&php=hypertext+processor foo=bar&baz=boom&cow=milk&php=hypertext+processor
2. Use loops to splice the arrays according to the URL parameter rules, and use "=" to splice the array units for keys and values. Just use "&" to splice.
Simple example:
function array_to_url_prarm($array) { $prarms = []; foreach ($array as $key => $val) { $prarms[] = $key . '=' . str_replace(' ', '+', $val); } return implode('&', $prarms); }
Recommended tutorial: "PHP Tutorial"
The above is the detailed content of How to convert array to URL parameters in PHP. For more information, please follow other related articles on the PHP Chinese website!