Is there a way to build a query automatically with http_build_query
using parameters with the same name?
If I do something like
array('foo' => 'x', 'foo' => 'y');
They are obviously overwritten within the array, but even if I do:
array('foo' => array('x', 'y'));
The function creates something like foo[0]=x&foo[1]
, which isn't what I want, since I need the parameters in this format foo=x&foo=y
.
$_SERVER['QUERY_STRING'] Returns the query string if the page is accessed via a query string. $_SERVER['HTTP_ACCEPT'] Returns the Accept header from the current request. $_SERVER['HTTP_ACCEPT_CHARSET']
The http_build_query() function is an inbuilt function in PHP which is used to generate URL-encoded query string from the associative (or indexed) array.
A query string commonly includes fields added to a base URL by a Web browser or other client application, for example as part of an HTML, choosing the appearance of a page, or jumping to positions in multimedia content.
An easy way to build a query string in Javascript is to use a URLSearchParams object: var query = new URLSearchParams(); query. append("KEY", "VALUE");
This should do what you want, I had an api that required the same thing.
$vars = array('foo' => array('x','y')); $query = http_build_query($vars, null, '&'); $string = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $query); //foo=x&foo=y
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With